index(p1) public

Returns the key for a given value. If not found, returns nil.

   h = { "a" => 100, "b" => 200 }
   h.index(200)   #=> "b"
   h.index(999)   #=> nil
Show source
Register or log in to add new notes.
May 27, 2009
0 thanks

Potentially slow operation

Remember that checking for a value is a potentially slow operation (all the elements might be iterated) as oposed to querying a key (e.g. with has_key?), which is supposed to be fast in a Hash.