Tintin81 Tintin81 - 1 year ago 53
Ruby Question

How to search nested array in Ruby?

I have this nested array:

array = [
["A", "X"],
["B", "Y"],
["C", "Z"]

Is there a function that returns "B" when I provide "Y" and "C" when I provide "Z"?

Answer Source

rassoc might be what you need.

array.rassoc("Y") would return ["B", "Y"] and you can use first to get only the "B".