ruby array loop always pair
Solution 1:
You might want to look at Enumerable
instead, which is included in Array
.
The method you want is Enumerable#each_slice
, which repeatedly yields from the enumerable the number of elements given (or less if there aren't that many at the end):
a = ['sda', 'sdb', 'sdc', 'sdd']
a.each_slice(2) do |b|
p b
end
Yields:
$ ruby slices.rb
["sda", "sdb"]
["sdc", "sdd"]
$