[Ruby] [Ruby] Has Hash#has_key? moved one step closer to the official deprecated?

1 minute read

Hash#has_key? is from Matz,

“has_key” has already been deprecated by “key?”

And declaration, it was pointed out as deprecated in various style guides for 8 years. It has been. For example,

However, officially, Hash#has_key? is still the only method used in the sample code, suppressing Hash#include?, Hash#key?, and Hash#member?. Yes, it is an activity that does not make you feel deprecated at all.

Japanese version is like this,

Japanese version

p({1 => "one"}.has_key?(1)) #=> true
p({1 => "one"}.has_key?(2)) #=> false

English version is like this.

English version

h = {"a" => 100, "b" => 200}
h.has_key?("a") #=> true
h.has_key?("z") #=> false

However, this is also up to Ruby 2.7, and from Ruby 2.8, it seems that the activity in the sample code will be handed over to other methods.

The latest comment on GitHub is like this.

 * call-seq:
 * hash.include?(key) -> true or false
 * hash.has_key?(key) -> true or false
 * hash.key?(key) -> true or false
 * hash.member?(key) -> true or false

 * Methods #has_key?, #key?, and #member? are aliases for \#include?.
 * Returns +true+ if +key+ is a key in +self+, otherwise +false+:
 * h = {foo: 0, bar: 1, baz: 2}
 * h.include?(:bar) # => true
 * h.include?(:nosuch) # => false

Hash#include? It feels like a push. However, Hash#has_key? is still in second place.

Personally, I am attached to Hash#has_key?, and sometimes I still use Hash#has_key? in the code that is discarded.

It’s not officially declared deprecated (isn’t it?) It’s OK to keep using Hash#has_key?, or you should make your heart devil and use other methods such as Hash#key? What? My heart sways.

If you really want to be deprecated, I would like you to give me guidance as soon as possible.