diff options
| author | Jun Hiroe <[email protected]> | 2014-03-27 14:04:30 +0900 |
|---|---|---|
| committer | Jun Hiroe <[email protected]> | 2014-03-27 14:09:29 +0900 |
| commit | 68695d174c3e4427353d6497b18507005b7a9507 (patch) | |
| tree | 389c269888ff09d2fdc7eb7f5855a81268098f6c /mrbgems/mruby-enum-ext/mrblib | |
| parent | 70718d5935ed0ae96ac3c782330e1195114b6463 (diff) | |
| download | mruby-68695d174c3e4427353d6497b18507005b7a9507.tar.gz mruby-68695d174c3e4427353d6497b18507005b7a9507.zip | |
Add Enumerable#zip
Diffstat (limited to 'mrbgems/mruby-enum-ext/mrblib')
| -rw-r--r-- | mrbgems/mruby-enum-ext/mrblib/enum.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/mrbgems/mruby-enum-ext/mrblib/enum.rb b/mrbgems/mruby-enum-ext/mrblib/enum.rb index ead9a794a..11c9dbd98 100644 --- a/mrbgems/mruby-enum-ext/mrblib/enum.rb +++ b/mrbgems/mruby-enum-ext/mrblib/enum.rb @@ -615,4 +615,34 @@ module Enumerable end nil end + + ## + # call-seq: + # enum.zip(arg, ...) -> an_array_of_array + # + # Takes one element from <i>enum</i> and merges corresponding + # elements from each <i>args</i>. This generates a sequence of + # <em>n</em>-element arrays, where <em>n</em> is one more than the + # count of arguments. The length of the resulting sequence will be + # <code>enum#size</code>. If the size of any argument is less than + # <code>enum#size</code>, <code>nil</code> values are supplied. + # + + def zip(*arg) + ary = [] + i = 0 + self.each do |val| + a = [] + a.push(val) + idx = 0 + while idx < arg.size + a2 = arg[idx].to_a + a.push(a2[i]) + idx += 1 + end + ary.push(a) + i += 1 + end + ary + end end |
