# Find has key from a value

**URL:** <https://www.ruby-forum.com/t/find-has-key-from-a-value/183300>\
**Category:** Ruby\
**Created:** [February 11, 2010, 12:54pm UTC](https://www.ruby-forum.com/t/find-has-key-from-a-value/183300 "2010-02-11T12:54:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 11, 2010, 12:54pm UTC](https://www.ruby-forum.com/t/find-has-key-from-a-value/183300/1 "2010-02-11T12:54:27Z")

</div>

Hi,

I cant seem to figure this out, if i have a hash value can i then get  
the key thats associated to that value.

So the hash.has\_value(val)? method will return true or false if that  
value exists in the hash but what i need to know is what is the key for  
the particular value.

Any ideas?

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 11, 2010, 1:28pm UTC](https://www.ruby-forum.com/t/find-has-key-from-a-value/183300/2 "2010-02-11T13:28:26Z")

</div>

> So the hash.has\_value(val)? method will return true or false if that

value exists in the hash but what i need to know is what is the key for

> the particular value.
> 
> Any ideas?

Yup, consider using Hash#invert or Hash#each\_pair.

The invert method returns a new hash where the key-value mappings have  
been  
inverted. For example:

some\_hash = {:a =\> 1, :b =\> 2, :c =\> 3}  
another\_hash = some\_hash.invert # gives {1 =\> :a, 2 =\> :b, 3 =\> :c}  
key\_for\_value = another\_hash[2] # gives :b

Each\_pair is a lot like each because it allows you to iterate through  
the  
data structure, but the block takes two arguments |key,value|. This  
means  
you could iterate through the values and when you find one for which you  
want to know the associated key, you’ll have it right there:

key\_for\_value = nil  
some\_hash.each\_pair do |letter,number|  
key\_for\_value = letter if number == 2  
end

If you’re doing this sort of thing a lot, you may want to consider a  
different data structure. It’s not efficient to keep inverting the keys  
and  
values or to iterate through the entire hash search for a particular  
pair.

-Jake

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 11, 2010, 1:34pm UTC](https://www.ruby-forum.com/t/find-has-key-from-a-value/183300/3 "2010-02-11T13:34:17Z")

</div>

2010/2/11 John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I cant seem to figure this out, if i have a hash value can i then get  
> the key thats associated to that value.
> 
> So the hash.has\_value(val)? method will return true or false if that  
> value exists in the hash but what i need to know is what is the key for  
> the particular value.
> 
> Any ideas?

Hash#rassoc

irb(main):001:0\> {:foo=\>:bar}.rassoc :bar  
=\> [:foo, :bar]  
irb(main):002:0\> {:foo=\>:bar}.rassoc :foo  
=\> nil  
irb(main):003:0\>

You can do

key, = hash.rassoc your\_value  
Note, the comma is important.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [February 11, 2010, 2:03pm UTC](https://www.ruby-forum.com/t/find-has-key-from-a-value/183300/4 "2010-02-11T14:03:40Z")

</div>

Thanks,
