0

I am using Rails 3.1.0 and I would like to get a particular value from a multidimensional array. That is, I have the following

array = [ ['Text1', 's1'], ['Text2', 's2'], ['Text3', 's3'] ] 

and, for example, I would like to search in the above array the string s3 so to get the corresponding value Text3. The same for s1 so to get the Text1 and for s2 so to get the Text2.

How can I make that?

0

1 Answer 1

3

For smallish arrays and infrequent lookups you can keep the array:

array = [ ['Text1', 's1'], ['Text2', 's2'], ['Text3', 's3'] ] p array.rassoc('s3').first #=> 'Text3' 
Sign up to request clarification or add additional context in comments.

1 Comment

+1, wow, never seen that method before... Is there a particular reason why this is included? Because I've never seen another case like this where this would actually be useful.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.