Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixing 1.9 spec: Add in ability for Array#zip to use #each if #to_ary…

… is not defined.
  • Loading branch information...
commit 9f3e430168f5ee399e22b4addbb9f382c39efdc7 1 parent 0b5914b
@subwindow subwindow authored
View
37 kernel/common/array.rb
@@ -1686,43 +1686,6 @@ def values_at(*args)
out
end
- # call-seq:
- # array.zip(arg, ...) -> an_array
- # array.zip(arg, ...) {| arr | block } -> nil
- #
- # Converts any arguments to arrays, then merges elements of
- # <i>self</i> with corresponding elements from each argument. This
- # generates a sequence of <code>self.size</code> <em>n</em>-element
- # arrays, where <em>n</em> is one more that the count of arguments. If
- # the size of any argument is less than <code>enumObj.size</code>,
- # <code>nil</code> values are supplied. If a block given, it is
- # invoked for each output array, otherwise an array of arrays is
- # returned.
- #
- # a = [ 4, 5, 6 ]
- # b = [ 7, 8, 9 ]
- # [1,2,3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
- # [1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8]]
- # a.zip([1,2],[8]) #=> [[4,1,8], [5,2,nil], [6,nil,nil]]
- #
- def zip(*others)
- out = Array.new(size) { [] }
- others = others.map { |ary| ary.to_ary }
-
- size.times do |i|
- slot = out.at(i)
- slot << @tuple.at(@start + i)
- others.each { |ary| slot << ary.at(i) }
- end
-
- if block_given?
- out.each { |ary| yield ary }
- return nil
- end
-
- out
- end
-
def unshift(*values)
return self if values.empty?
View
39 kernel/common/array18.rb
@@ -19,7 +19,7 @@ def choice(n=undefined)
result[n..size] = []
result
end
-
+
# Recursively flatten any contained Arrays into an one-dimensional result.
# The optional level argument determines the level of recursion to flatten
def flatten(level=-1)
@@ -131,4 +131,41 @@ def uniq!
self
end
+
+ # call-seq:
+ # array.zip(arg, ...) -> an_array
+ # array.zip(arg, ...) {| arr | block } -> nil
+ #
+ # Converts any arguments to arrays, then merges elements of
+ # <i>self</i> with corresponding elements from each argument. This
+ # generates a sequence of <code>self.size</code> <em>n</em>-element
+ # arrays, where <em>n</em> is one more that the count of arguments. If
+ # the size of any argument is less than <code>enumObj.size</code>,
+ # <code>nil</code> values are supplied. If a block given, it is
+ # invoked for each output array, otherwise an array of arrays is
+ # returned.
+ #
+ # a = [ 4, 5, 6 ]
+ # b = [ 7, 8, 9 ]
+ # [1,2,3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
+ # [1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8]]
+ # a.zip([1,2],[8]) #=> [[4,1,8], [5,2,nil], [6,nil,nil]]
+ #
+ def zip(*others)
+ out = Array.new(size) { [] }
+ others = others.map { |ary| ary.to_ary }
+
+ size.times do |i|
+ slot = out.at(i)
+ slot << @tuple.at(@start + i)
+ others.each { |ary| slot << ary.at(i) }
+ end
+
+ if block_given?
+ out.each { |ary| yield ary }
+ return nil
+ end
+
+ out
+ end
end
View
53 kernel/common/array19.rb
@@ -5,7 +5,7 @@ class Array
def self.try_convert(obj)
Rubinius::Type.try_convert obj, Array, :to_ary
end
-
+
def flatten(level=-1)
level = Rubinius::Type.coerce_to(level, Integer, :to_int)
return self.dup if level == 0
@@ -126,12 +126,12 @@ def select!(&block)
ary = select(&block)
replace ary unless size == ary.size
end
-
+
def sort_by!(&block)
Rubinius.check_frozen
-
+
return to_enum :sort_by! unless block_given?
-
+
replace sort_by(&block)
end
@@ -154,4 +154,49 @@ def uniq!
self
end
+
+ # call-seq:
+ # ary.zip(arg, ...) -> new_ary
+ # ary.zip(arg, ...) {| arr | block } -> nil
+ #
+ # Converts any arguments to arrays, then merges elements of
+ # +self+ with corresponding elements from each argument. This
+ # generates a sequence of <code>self.size</code> <em>n</em>-element
+ # arrays, where <em>n</em> is one more that the count of arguments. If
+ # the size of any argument is less than <code>enumObj.size</code>,
+ # <code>nil</code> values are supplied. If a block is given, it is
+ # invoked for each output array, otherwise an array of arrays is
+ # returned.
+ #
+ # a = [ 4, 5, 6 ]
+ # b = [ 7, 8, 9 ]
+ # [1,2,3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
+ # [1,2].zip(a,b) #=> [[1, 4, 7], [2, 5, 8]]
+ # a.zip([1,2],[8]) #=> [[4,1,8], [5,2,nil], [6,nil,nil]]
+ #
+ def zip(*others)
+ out = Array.new(size) { [] }
+ others = others.map do |ary|
+ if ary.respond_to?(:to_ary)
+ ary.to_ary
+ else
+ elements = []
+ ary.each { |e| elements << e }
+ elements
+ end
+ end
+
+ size.times do |i|
+ slot = out.at(i)
+ slot << @tuple.at(@start + i)
+ others.each { |ary| slot << ary.at(i) }
+ end
+
+ if block_given?
+ out.each { |ary| yield ary }
+ return nil
+ end
+
+ out
+ end
end
View
2  spec/ruby/core/array/zip_spec.rb
@@ -35,7 +35,7 @@ def each(&b)
[3,4].each(&b)
end
end.new
-
+
[1, 2].zip(obj).should == [[1, 3], [2, 4]]
end
end
View
1  spec/tags/19/ruby/core/array/zip_tags.txt
@@ -1 +0,0 @@
-fails:Array#zip uses #each to extract arguments' elements when #to_ary fails
Please sign in to comment.
Something went wrong with that request. Please try again.