Tintin81 Tintin81 - 2 months ago 4x
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"?


rassoc might be what you need.

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