I would do it like this:
films = [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]
films.each do |arr|
puts arr.join(' : ')
end
--output:--
one : vol1
one : vol2
three : vol3
And if you want to save the strings in a new_array--rather than print
them--you can use map():
films = [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]
results = films.map do |arr|
arr.join(' : ')
end
p results
--output:--
["one : vol1", "one : vol2", "three : vol3"]
Array#join() and String#split() should be in every beginner's arsenal.
As for map(), it sends each element of the array to the block, and then
shoves the return value of the block into a new array. Here is a
simpler example:
films = [["one", "vol1"], ["one", "vol2"], ["three", "vol3"]]
results = films.map do |arr|
"hello"
end
p results
--output:--
["hello", "hello", "hello"]
For each element of the array, the block returns one value, which is
stored in a new array.
--
Posted via http://www.ruby-forum.com/.