Skip to content

Commit 72550d2

Browse files
[DOC] Doc for Array#zip (ruby#11961)
1 parent dccfab0 commit 72550d2

File tree

1 file changed

+62
-32
lines changed

1 file changed

+62
-32
lines changed

array.c

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4469,65 +4469,95 @@ take_items(VALUE obj, long n)
44694469

44704470
/*
44714471
* call-seq:
4472-
* array.zip(*other_arrays) -> new_array
4473-
* array.zip(*other_arrays) {|other_array| ... } -> nil
4472+
* zip(*other_arrays) -> new_array
4473+
* zip(*other_arrays) {|other_array| ... } -> nil
44744474
*
4475-
* When no block given, returns a new +Array+ +new_array+ of size <tt>self.size</tt>
4476-
* whose elements are Arrays.
4475+
* With no block given, combines +self+ with the collection of +other_arrays+;
4476+
* returns a new array of sub-arrays:
44774477
*
4478-
* Each nested array <tt>new_array[n]</tt> is of size <tt>other_arrays.size+1</tt>,
4479-
* and contains:
4478+
* [0, 1].zip(['zero', 'one'], [:zero, :one])
4479+
* # => [[0, "zero", :zero], [1, "one", :one]]
44804480
*
4481-
* - The _nth_ element of +self+.
4482-
* - The _nth_ element of each of the +other_arrays+.
4481+
* Returned:
44834482
*
4484-
* If all +other_arrays+ and +self+ are the same size:
4483+
* - The outer array is of size <tt>self.size</tt>.
4484+
* - Each sub-array is of size <tt>other_arrays.size + 1</tt>.
4485+
* - The _nth_ sub-array contains (in order):
4486+
*
4487+
* - The _nth_ element of +self+.
4488+
* - The _nth_ element of each of the other arrays, as available.
4489+
*
4490+
* Example:
4491+
*
4492+
* a = [0, 1]
4493+
* zipped = a.zip(['zero', 'one'], [:zero, :one])
4494+
* # => [[0, "zero", :zero], [1, "one", :one]]
4495+
* zipped.size # => 2 # Same size as a.
4496+
* zipped.first.size # => 3 # Size of other arrays plus 1.
4497+
*
4498+
* When the other arrays are all the same size as +self+,
4499+
* the returned sub-arrays are a rearrangement containing exactly elements of all the arrays
4500+
* (including +self+), with no omissions or additions:
44854501
*
44864502
* a = [:a0, :a1, :a2, :a3]
44874503
* b = [:b0, :b1, :b2, :b3]
44884504
* c = [:c0, :c1, :c2, :c3]
44894505
* d = a.zip(b, c)
4490-
* d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
4506+
* pp d
4507+
* # =>
4508+
* [[:a0, :b0, :c0],
4509+
* [:a1, :b1, :c1],
4510+
* [:a2, :b2, :c2],
4511+
* [:a3, :b3, :c3]]
44914512
*
4492-
* If any array in +other_arrays+ is smaller than +self+,
4493-
* fills to <tt>self.size</tt> with +nil+:
4513+
* When one of the other arrays is smaller than +self+,
4514+
* pads the corresponding sub-array with +nil+ elements:
44944515
*
44954516
* a = [:a0, :a1, :a2, :a3]
44964517
* b = [:b0, :b1, :b2]
44974518
* c = [:c0, :c1]
44984519
* d = a.zip(b, c)
4499-
* d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]
4520+
* pp d
4521+
* # =>
4522+
* [[:a0, :b0, :c0],
4523+
* [:a1, :b1, :c1],
4524+
* [:a2, :b2, nil],
4525+
* [:a3, nil, nil]]
45004526
*
4501-
* If any array in +other_arrays+ is larger than +self+,
4502-
* its trailing elements are ignored:
4527+
* When one of the other arrays is larger than +self+,
4528+
* _ignores_ its trailing elements:
45034529
*
45044530
* a = [:a0, :a1, :a2, :a3]
45054531
* b = [:b0, :b1, :b2, :b3, :b4]
45064532
* c = [:c0, :c1, :c2, :c3, :c4, :c5]
45074533
* d = a.zip(b, c)
4508-
* d # => [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]
4534+
* pp d
4535+
* # =>
4536+
* [[:a0, :b0, :c0],
4537+
* [:a1, :b1, :c1],
4538+
* [:a2, :b2, :c2],
4539+
* [:a3, :b3, :c3]]
45094540
*
4510-
* If an argument is not an array, it extracts the values by calling #each:
4511-
*
4512-
* a = [:a0, :a1, :a2, :a2]
4513-
* b = 1..4
4514-
* c = a.zip(b)
4515-
* c # => [[:a0, 1], [:a1, 2], [:a2, 3], [:a2, 4]]
4516-
*
4517-
* When a block is given, calls the block with each of the sub-arrays (formed as above); returns +nil+:
4541+
* With a block given, calls the block with each of the other arrays;
4542+
* returns +nil+:
45184543
*
4544+
* d = []
45194545
* a = [:a0, :a1, :a2, :a3]
45204546
* b = [:b0, :b1, :b2, :b3]
45214547
* c = [:c0, :c1, :c2, :c3]
4522-
* a.zip(b, c) {|sub_array| p sub_array} # => nil
4523-
*
4524-
* Output:
4525-
*
4526-
* [:a0, :b0, :c0]
4527-
* [:a1, :b1, :c1]
4528-
* [:a2, :b2, :c2]
4529-
* [:a3, :b3, :c3]
4548+
* a.zip(b, c) {|sub_array| d.push(sub_array.reverse) } # => nil
4549+
* pp d
4550+
* # =>
4551+
* [[:c0, :b0, :a0],
4552+
* [:c1, :b1, :a1],
4553+
* [:c2, :b2, :a2],
4554+
* [:c3, :b3, :a3]]
4555+
*
4556+
* For an *object* in *other_arrays* that is not actually an array,
4557+
* forms the the "other array" as <tt>object.to_ary</tt>, if defined,
4558+
* or as <tt>object.each.to_a</tt> otherwise.
45304559
*
4560+
* Related: see {Methods for Converting}[rdoc-ref:Array@Methods+for+Converting].
45314561
*/
45324562

45334563
static VALUE

0 commit comments

Comments
 (0)