array = ["A", "B", "C", "D"] array => ["A", "B", "C", "D"] array.each_slice(2).to_a => [["A", "B"], ["C", "D"]] array.each_slice(2).to_a.length => 2
You might expect array.each_slice(2).to_a
change your original array
, but here you will have a new array
object, because each_slice
is a non-destructive method, like most in ruby.
new_array = array.each_slice(2).to_a new_array => [["A", "B"], ["C", "D"]] new_array[0] => ["A", "B"]
source share