Forum: Ruby want to search value in nested arrays and return other value

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8d1abb557a221370f99faf31f4953f82?d=identicon&s=25 James Red (rubyforum)
on 2008-12-10 00:04
Hi.

I have following problem:

VALUES = [['value 1', 1], ['value 2', 2], ['value 3', 3]]

I need a function that returns 'value 1' if I call the function with
parameter 1.

I tried this:

def value_name(value)
  VALUES.select {|v| v[1] == 1}[0][0]
end

so value_name(1) returns 'value 1' but is there something more error
tolerant? E.g. returning '' if value isn't present instead of throwing
an error?

Changing the structure of VALUES is no option, I need exactly this
structure for options_for_select from the Rails framework

Thanks in advance
Andreas
59193d8082ee0fe75675d3bb376cf2ba?d=identicon&s=25 Pierre Pierre (pierre_p)
on 2008-12-10 01:42
Are you the one defining the data structure?
Or is it something you just have to use the way it is?

Because in this case, a hashmap would make more sense I believe.
797ef431a5e1295b56c08e1db4c8d2df?d=identicon&s=25 botp (Guest)
on 2008-12-10 02:31
(Received via mailing list)
On Wed, Dec 10, 2008 at 6:58 AM, Joao Silva > I tried this:
> def value_name(value)
>  VALUES.select {|v| v[1] == 1}[0][0]
> end

did you really check that? it will always return "value 1" ;)

> so value_name(1) returns 'value 1' but is there something more error
> tolerant? E.g. returning '' if value isn't present instead of throwing an error?
>

then just create it
eg,

> def value_name3(value)
>   VALUES.each{|k,v| return k if v== value}
>   ""
> end
=> nil
> value_name3 1
=> "value 1"
> value_name3 2
=> "value 2"
> value_name3 3
=> "value 3"
> value_name3 4
=> ""
> value_name3 "asdf"
=> ""
8d1abb557a221370f99faf31f4953f82?d=identicon&s=25 James Red (rubyforum)
on 2008-12-10 11:24
Pierre Pat wrote:
> Are you the one defining the data structure?
> Or is it something you just have to use the way it is?
>
> Because in this case, a hashmap would make more sense I believe.
I need exactly this structure because it is used in options_for_select()
in Rails Framework, so i can't change this. A hash is unsorted, and I
need this sorted.

botp wrote:
> did you really check that? it will always return "value 1" ;)
erm, right :) I meant 'v[1] == value' - thats how I tried it out on my
computer :)

> def value_name3(value)
>   VALUES.each{|k,v| return k if v== value}
>   ""
> end
Thank you very much, that does the task and works as I wanted to have
it.
Cf7cd97cdc8ed7d4ae92965b24f0dfad?d=identicon&s=25 Stefan Rusterholz (apeiros)
on 2008-12-10 11:48
Joao Silva wrote:
> Hi.
>
> I have following problem:
>
> VALUES = [['value 1', 1], ['value 2', 2], ['value 3', 3]]
>
> I need a function that returns 'value 1' if I call the function with
> parameter 1.

Try .rassoc(1).first

Regards
Stefan
This topic is locked and can not be replied to.