Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
79dd12ba
Commit
79dd12ba
authored
Mar 19, 2013
by
skandhas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add Enumerable#take, Enumerable#take_while
parent
9262a9f4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
0 deletions
+51
-0
mrbgems/mruby-enum-ext/mrblib/enum.rb
mrbgems/mruby-enum-ext/mrblib/enum.rb
+41
-0
mrbgems/mruby-enum-ext/test/enum.rb
mrbgems/mruby-enum-ext/test/enum.rb
+10
-0
No files found.
mrbgems/mruby-enum-ext/mrblib/enum.rb
View file @
79dd12ba
...
...
@@ -40,5 +40,46 @@ module Enumerable
end
ary
end
##
# call-seq:
# enum.take(n) -> array
#
# Returns first n elements from <i>enum</i>.
#
# a = [1, 2, 3, 4, 5, 0]
# a.take(3) #=> [1, 2, 3]
def
take
(
n
)
raise
TypeError
,
"expected Integer for 1st argument"
unless
n
.
kind_of?
Integer
raise
ArgumentError
,
"attempt to take negative size"
if
n
<
0
ary
=
[]
self
.
each
do
|
e
|
break
if
ary
.
size
>=
n
ary
<<
e
end
ary
end
##
# call-seq:
# enum.take_while {|arr| block } -> array
#
# Passes elements to the block until the block returns +nil+ or +false+,
# then stops iterating and returns an array of all prior elements.
#
#
# a = [1, 2, 3, 4, 5, 0]
# a.take_while {|i| i < 3 } #=> [1, 2]
def
take_while
(
&
block
)
ary
=
[]
self
.
each
do
|
e
|
return
ary
unless
block
.
call
(
e
)
ary
<<
e
end
ary
end
end
mrbgems/mruby-enum-ext/test/enum.rb
View file @
79dd12ba
...
...
@@ -13,3 +13,13 @@ assert("Enumrable#drop_while") do
assert_equal
a
.
drop_while
{
|
i
|
i
<
3
},
[
3
,
4
,
5
,
0
]
end
assert
(
"Enumrable#take"
)
do
a
=
[
1
,
2
,
3
,
4
,
5
,
0
]
assert_equal
a
.
take
(
3
),
[
1
,
2
,
3
]
end
assert
(
"Enumrable#take_while"
)
do
a
=
[
1
,
2
,
3
,
4
,
5
,
0
]
assert_equal
a
.
take_while
{
|
i
|
i
<
3
},
[
1
,
2
]
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment