bootrails.com is now moving to saaslit.com  See you there!
bootrails moves to  saaslit.com
Post

How to check if hash has key in Ruby

With Ruby, there are some cases where you want to check if a given key is already in the hash.

If you’re new to hashes, I wrote a basic blog article about Ruby hashes.

Is checking the key of a hash already exists in the Ruby API ?

What if you want to check that a given hash has a given key ? Before writing any code, the first thing you should think about is “does this already exist somewhere in the API” ?

It’s very uncommon that a utility function is missing - excerpt may be for JavaScript, for historical reasons.

But not for Ruby, even more in a Ruby-on-Rails environment.

Ruby core : a one-liner way to check if key exists

For those who already know Ruby, here is the quick answer

1
$> my_hash.key?("my_key")

Ruby : Check presence of key by casting to a boolean

Start the Ruby console (in a Rails project, enter “bin/rails console”).

1
$> my_hash = {'a' => 40, a: 41, b: 'some_value'}

Note by the way that the String 'a' is considered as a different key than symbol a:

I want to check that the key belongs to my_hash, so a naive approach would be to use the double !! that will convert any value to a boolean. It’s a trick used in other languages like JavaScript.

1
2
3
4
5
6
7
8
$> my_hash[:b]
'some_value'
$> my_hash[:c]
nil
$> !!my_hash[:b]
false
$> !!my_hash[:c]
true

A more expressive way to check presence of a specific key of a Ruby hash

The Ruby hash has an expressive API for the most common use cases. You can use the .key? method instead of the !! seen above.

It’s a lot more readable for other developers.

Examples :

1
2
3
4
5
6
7
8
$> my_hash[:b]
'some_value'
$> my_hash[:c]
nil
$> my_hash.key?[:b]
false
$> my_hash.key?[:c]
true

Ruby-on-Rails : check if hash has key

You can use the .has_key? as well. I prefer this solution because it makes the intent a lot clearer. Since you expect a boolean, starting the method by is_ or has_/have_ is better.

1
2
3
4
5
6
7
8
$> my_hash[:b]
'some_value'
$> my_hash[:c]
nil
$> my_hash.has_key?[:b]
false
$> my_hash.has_key?[:c]
true

The most robust possible answer

So now what if you’re not quite sure about the hash you’re checking.

Case one : You’re not sure if the keys are symbols or not - think about a hash that is extracted from a remote API.

1
2
$> my_hash.deep_symbolize_keys.try(:key?, :a)
true

Case two : you’re not sure if it is actually a hash (could be nil or a wrong type)

1
2
$> my_hash.try(:key?, :a)
true

Note that this could be considered as over-engineering, depending on the context.

Conclusion

Checking if a hash has a key in Ruby is pretty straightforward, all you need is to pay a little attention in which context you’re writing this verification.

See also this SO question.

This post is licensed under CC BY 4.0 by the author.