Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4619

Destructuring in a Ruby block when using index

$
0
0

Without using index I was able to destructure the first and second elements of an Array of Arrays like so:

[ [ 1, 2 ], [ 3, 4 ] ].each do |first, second|  puts secondend#=> 2#=> 4

I then needed to get the index for each iteration so I used .with_index and I assumed the index would just get added as the last argument of the block:

[ [ 1, 2 ], [ 3, 4 ] ].each.with_index do |first, second, index|  puts secondend#=> 0#=> 1

However, it was the index. The first value is the entire Array, not destructured.

How do you use .with_index and still destructure the Array?


Viewing all articles
Browse latest Browse all 4619

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>