I am using Ruby on Rails 3.2.2 and Ruby 1.9.2.
Given the following multidimensional Array:
[["value1", "value1_other"], ["value2", "value2_other"], ["value3", "value3_other"]]
I would like to get (note: I would like to “extract” only the first value of all “nested” Arrays):
["value1", "value2", "value3"]
How can I make that in a smart way?
You can use
Array#collectto execute a block for each element of the outer array. To get the first element, pass a block that indexes the array.In use:
Instead of
{|ind| ind[0]}, you can useArray#firstto get the first element of each inner array:For the
&:firstsyntax, read “Ruby/Ruby on Rails ampersand colon shortcut“.