Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>dogs = names.zip(breeds).map { |name, breed| Dog.new(name, breed) } </code></pre> <p><code>Array#zip</code> interleaves the target array with elements of the arguments, so</p> <pre><code>irb&gt; [1, 2, 3].zip(['a', 'b', 'c']) #=&gt; [ [1, 'a'], [2, 'b'], [3, 'c'] ] </code></pre> <p>You can use arrays of different lengths (in which case the target array determines the length of the resulting array, with the extra entries filled in with <code>nil</code>).</p> <pre><code>irb&gt; [1, 2, 3, 4, 5].zip(['a', 'b', 'c']) #=&gt; [ [1, 'a'], [2, 'b'], [3, 'c'], [4, nil], [5, nil] ] irb&gt; [1, 2, 3].zip(['a', 'b', 'c', 'd', 'e']) #=&gt; [ [1, 'a'], [2, 'b'], [3, 'c'] ] </code></pre> <p>You can also zip more than two arrays together:</p> <pre><code>irb&gt; [1,2,3].zip(['a', 'b', 'c'], [:alpha, :beta, :gamma]) #=&gt; [ [1, 'a', :alpha], [2, 'b', :beta], [3, 'c', :gamma] ] </code></pre> <p><code>Array#map</code> is a great way to transform an array, since it returns an array where each entry is the result of running the block on the corresponding entry in the target array.</p> <pre><code>irb&gt; [1,2,3].map { |n| 10 - n } #=&gt; [ 9, 8, 7 ] </code></pre> <p>When using iterators over arrays of arrays, if you give a multiple parameter block, the array entries will be automatically broken into those parameters:</p> <pre><code>irb&gt; [ [1, 'a'], [2, 'b'], [3, 'c'] ].each { |array| p array } [ 1, 'a' ] [ 2, 'b' ] [ 3, 'c' ] #=&gt; nil irb&gt; [ [1, 'a'], [2, 'b'], [3, 'c'] ].each do |num, char| ...&gt; puts "number: #{num}, character: #{char}" ...&gt; end number 1, character: a number 2, character: b number 3, character: c #=&gt; [ [1, 'a'], [2, 'b'], [3, 'c'] ] </code></pre> <p>Like <a href="https://stackoverflow.com/users/10771/matt-briggs">Matt Briggs</a> <a href="https://stackoverflow.com/questions/2648824/basic-array-iteration-in-ruby/2648850#2648850">mentioned</a>, <code>#each_with_index</code> is another good tool to know about. It iterates through the elements of an array, passing a block each element in turn.</p> <pre><code>irb&gt; ['a', 'b', 'c'].each_with_index do |char, index| ...&gt; puts "character #{char} at index #{index}" ...&gt; end character a at index 0 character b at index 1 character c at index 2 #=&gt; [ 'a', 'b', 'c' ] </code></pre> <p>When using an iterator like <code>#each_with_index</code> you can use parentheses to break up array elements into their constituent parts:</p> <pre><code>irb&gt; [ [1, 'a'], [2, 'b'], [3, 'c'] ].each_with_index do |(num, char), index| ...&gt; puts "number: #{num}, character: #{char} at index #{index}" ...&gt; end number 1, character: a at index 0 number 2, character: b at index 1 number 3, character: c at index 2 #=&gt; [ [1, 'a'], [2, 'b'], [3, 'c'] ] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload