diff options
| author | Jun Hiroe <[email protected]> | 2014-03-24 16:13:27 +0900 |
|---|---|---|
| committer | Jun Hiroe <[email protected]> | 2014-03-24 16:26:23 +0900 |
| commit | 7e41e2ed0e087f7988ba399f5936aeee649ac231 (patch) | |
| tree | ca266b9dfab1bffc0d5399dd030605bbdc0f6445 /mrbgems/mruby-enum-ext/mrblib/enum.rb | |
| parent | b8d7f1ce7aaef75d43f515231fcd62dfe34116d5 (diff) | |
| download | mruby-7e41e2ed0e087f7988ba399f5936aeee649ac231.tar.gz mruby-7e41e2ed0e087f7988ba399f5936aeee649ac231.zip | |
Enumerable#each_with_object
Diffstat (limited to 'mrbgems/mruby-enum-ext/mrblib/enum.rb')
| -rw-r--r-- | mrbgems/mruby-enum-ext/mrblib/enum.rb | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb index 885ec26db..02513d1ad 100644 --- a/mrbgems/mruby-enum-ext/mrblib/enum.rb +++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb @@ -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 |
