Unverified Commit 3a22d113 authored by ksss's avatar ksss

Reimplement `Enumerable#zip` with Enumerator

for fix some specs
- [passes each element of the result array to a block and return nil if a block is given](https://github.com/ruby/spec/blob/a585ec35d185435e5c11f371ba4ed2a29d8817bd/core/enumerable/zip_spec.rb#L11-L17)
- [converts arguments to arrays using #to_ary](https://github.com/ruby/spec/blob/a585ec35d185435e5c11f371ba4ed2a29d8817bd/core/enumerable/zip_spec.rb#L23-L27)
- [converts arguments to enums using #to_enum](https://github.com/ruby/spec/blob/a585ec35d185435e5c11f371ba4ed2a29d8817bd/core/enumerable/zip_spec.rb#L29-L34)
- [gathers whole arrays as elements when each yields multiple](https://github.com/ruby/spec/blob/a585ec35d185435e5c11f371ba4ed2a29d8817bd/core/enumerable/zip_spec.rb#L36-L39)
parent 203b071c
......@@ -621,25 +621,40 @@ end
module Enumerable
# use Enumerator to use infinite sequence
def zip(*arg)
ary = []
arg = arg.map{|a|a.each}
i = 0
self.each do |*val|
a = []
a.push(val.__svalue)
idx = 0
while idx < arg.size
begin
a.push(arg[idx].next)
rescue StopIteration
a.push(nil)
def zip(*args, &block)
args = args.map do |a|
if a.respond_to?(:to_ary)
a.to_ary.to_enum(:each)
elsif a.respond_to?(:each)
a.to_enum(:each)
else
raise TypeError, "wrong argument type #{a.class} (must respond to :each)"
end
end
result = block ? nil : []
each do |*val|
tmp = [val.__svalue]
args.each do |arg|
v = if arg.nil?
nil
else
begin
arg.next
rescue StopIteration
nil
end
end
idx += 1
tmp.push(v)
end
if result.nil?
block.call(tmp)
else
result.push(tmp)
end
ary.push(a)
i += 1
end
ary
result
end
end
......@@ -544,3 +544,13 @@ assert 'Range#each' do
end
assert_equal [1,2,3,4,5], c
end
assert 'Enumerable#zip' do
assert_equal [[1, 10], [2, 11], [3, 12]], [1,2,3].zip(10..Float::INFINITY)
ret = []
assert_equal nil, [1,2,3].zip(10..Float::INFINITY) { |i| ret << i }
assert_equal [[1, 10], [2, 11], [3, 12]], ret
assert_raise(TypeError) { [1].zip(1) }
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment