Hello, I am relatively new to Ruby. I have an existing array:
a = [1 , 2 , 3 , 4 , 5 , 6]
and I want to create this 2-D array:
b = [[1 , 2][2 , 4][5 , 6]]
I am using the following code, which works, but seems somewhat clumsy:
b = Array.new
i = 0
k = 0
for item in a
if i == 0
a1 = item
i = 1
elsif i == 1
a2 = item
i = 0
b[k] = [a1, a2]
puts "b..#{k}, #{b[k]}"
k = k + 1
end
end
I would appreciate any advice to make this code look more elegant or
ruby-like. In particular, I suspect there is a way to avoid the
indices.
Thanks for the help.
···
--
Posted via http://www.ruby-forum.com/.