Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 47 additions & 31 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4373,65 +4373,81 @@ take_items(VALUE obj, long n)

/*
* call-seq:
* array.zip(*other_arrays) -> new_array
* array.zip(*other_arrays) {|other_array| ... } -> nil
* zip(*objects) -> new_array
* zip(*objects) {|other_array| ... } -> nil
*
* When no block given, returns a new +Array+ +new_array+ of size <tt>self.size</tt>
* whose elements are Arrays.
* For an *object* in *objects* that is not an array,
* forms the the "other array" as <tt>object.to_ary</tt>, if defined,
* or as <tt>object.each.to_a</tt> otherwise.
*
* Each nested array <tt>new_array[n]</tt> is of size <tt>other_arrays.size+1</tt>,
* and contains:
* With no block given, combines +self+ with the collection of other arrays;
* returns a new array of sub-arrays.
*
* The returned object is an array of size <tt>objects.size + 1</tt>
* (that is, the count of other arrays plus one), and contains:
*
* - The _nth_ element of +self+.
* - The _nth_ element of each of the +other_arrays+.
* - The _nth_ element of each of the other arrays, as available.
*
* If all +other_arrays+ and +self+ are the same size:
* When the other arrays are all the same size as +self+,
* the returned sub-arrays are a rearrangement containing exactly elements of all the arrays
* (including +self+), with no omissions or additions:
*
* a = [:a0, :a1, :a2, :a3]
* b = [:b0, :b1, :b2, :b3]
* c = [:c0, :c1, :c2, :c3]
* d = a.zip(b, c)
* d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
* pp d
* # =>
* [[:a0, :b0, :c0],
* [:a1, :b1, :c1],
* [:a2, :b2, :c2],
* [:a3, :b3, :c3]]
*
* If any array in +other_arrays+ is smaller than +self+,
* fills to <tt>self.size</tt> with +nil+:
* When one of the other arrays is smaller than +self+,
* pads the corresponding sub-array with +nil+ elements:
*
* a = [:a0, :a1, :a2, :a3]
* b = [:b0, :b1, :b2]
* c = [:c0, :c1]
* d = a.zip(b, c)
* d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
* pp d
* # =>
* [[:a0, :b0, :c0],
* [:a1, :b1, :c1],
* [:a2, :b2, nil],
* [:a3, nil, nil]]
*
* If any array in +other_arrays+ is larger than +self+,
* its trailing elements are ignored:
* When one of the other arrays is larger than +self+,
* _ignores_ its trailing elements:
*
* a = [:a0, :a1, :a2, :a3]
* b = [:b0, :b1, :b2, :b3, :b4]
* c = [:c0, :c1, :c2, :c3, :c4, :c5]
* d = a.zip(b, c)
* d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
*
* If an argument is not an array, it extracts the values by calling #each:
* pp d
* # =>
* [[:a0, :b0, :c0],
* [:a1, :b1, :c1],
* [:a2, :b2, :c2],
* [:a3, :b3, :c3]]
*
* a = [:a0, :a1, :a2, :a2]
* b = 1..4
* c = a.zip(b)
* c # => [[:a0, 1], [:a1, 2], [:a2, 3], [:a2, 4]]
*
* When a block is given, calls the block with each of the sub-arrays (formed as above); returns +nil+:
* With a block given, calls the block with each of the other arrays;
* returns +nil+:
*
* d = []
* a = [:a0, :a1, :a2, :a3]
* b = [:b0, :b1, :b2, :b3]
* c = [:c0, :c1, :c2, :c3]
* a.zip(b, c) {|sub_array| p sub_array} # => nil
*
* Output:
*
* [:a0, :b0, :c0]
* [:a1, :b1, :c1]
* [:a2, :b2, :c2]
* [:a3, :b3, :c3]
* a.zip(b, c) {|sub_array| d.push(sub_array.reverse) } # => nil
* pp d
* # =>
* [[:c0, :b0, :a0],
* [:c1, :b1, :a1],
* [:c2, :b2, :a2],
* [:c3, :b3, :a3]]
*
* Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
*/

static VALUE
Expand Down