I am unsure how to select the second item from each array in a two-dimensional array given a condition on the first. Here is a similar example to what I am trying to accomplish:
If you have a 2-dimensional array in Ruby, [[1,'a'],[2,'b'],[3,'c'],[4,'d']], how could you create an array with only the letters that are in an array with an even number? (Assuming each sub-array has the same format: [number, letter])
Although this code does not work, I assumed a solution would be similar to:
array1 = [[1,'a'],[2,'b'],[3,'c'],[4,'d']]
array2 = array1.each do |num, letter|
if num.even?
return letter
end
end
I want the value of array2 after running this to be ['b', 'd'].