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
7e41e2ed
Commit
7e41e2ed
authored
Mar 24, 2014
by
Jun Hiroe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enumerable#each_with_object
parent
b8d7f1ce
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
0 deletions
+27
-0
mrbgems/mruby-enum-ext/mrblib/enum.rb
mrbgems/mruby-enum-ext/mrblib/enum.rb
+23
-0
mrbgems/mruby-enum-ext/test/enum.rb
mrbgems/mruby-enum-ext/test/enum.rb
+4
-0
No files found.
mrbgems/mruby-enum-ext/mrblib/enum.rb
View file @
7e41e2ed
...
...
@@ -475,4 +475,27 @@ module Enumerable
count
==
1
?
true
:
false
end
##
# call-seq:
# enum.each_with_object(obj) { |(*args), memo_obj| ... } -> obj
# enum.each_with_object(obj) -> an_enumerator
#
# Iterates the given block for each element with an arbitrary
# object given, and returns the initially given object.
#
# If no block is given, returns an enumerator.
#
# (1..10).each_with_object([]) { |i, a| a << i*2 }
# #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
#
def
each_with_object
(
obj
=
nil
,
&
block
)
raise
ArgumentError
,
"wrong number of arguments (0 for 1)"
if
obj
==
nil
return
to_enum
:each_with_object
unless
block_given?
self
.
each
{
|*
val
|
block
.
call
(
*
val
,
obj
)
}
obj
end
end
mrbgems/mruby-enum-ext/test/enum.rb
View file @
7e41e2ed
...
...
@@ -111,3 +111,7 @@ assert("Enumerable#one?") do
assert_true
[
nil
,
true
,
false
].
one?
end
assert
(
"Enumerable#each_with_object"
)
do
assert_true
[
2
,
4
,
6
,
8
,
10
,
12
,
14
,
16
,
18
,
20
],
(
1
..
10
).
each_with_object
([])
{
|
i
,
a
|
a
<<
i
*
2
}
assert_raise
(
ArgumentError
)
{
(
1
..
10
).
each_with_object
()
{
|
i
,
a
|
a
<<
i
*
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