Given this multidimensional array:
a = [ [0,'abc'], [1,'def'] ]
I want to return just the keys (where keys are index 0s of subarray) and just the values (where values are index 1s of subarray):
a.keys
# => [0,1]
a.values
# => ['abc','def']
My solution to this is to use map to return a collection and use shift and pop to return the correct items:
a.map(&:shift)
=> [0, 1]
a.map(&:pop)
=> ["abc", "def"]
Great it works, but then I noticed the side effects:
a
=> [[], []]
Is there a way to get the functionality I have but without the side effects?
Hash[a]will give you back{0 => "abc", 1 => "def"}. Is that what you're going for?