Post

The Elements of Style in Ruby #9: Hash#has_key? and Hash#has_value? are deprecated

One can often find similar code in the wild:

1
some_hash.has_key?(some_key)

has_something? predicates are not idiomatic in Ruby and Hash#has_key? and Hash#has_value? are nothing, but remnants of the early days of Ruby. In fact, according to Matz, they are deprecated in favor of Hash#key? and Hash#value?.

The above snippet should look like:

1
some_hash.key?(some_key)

As usual - you can use RuboCop to identify and fix such shortfalls of your code.

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