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

Hash # has_key? is from Matz

"has_key" has already been deprecated by "key?"

Sentence It has been 8 years since then, and various Style guides pointed out that it was deprecated. It has been. For example

But 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 looks like this,

Japanese version


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

English version looks like this.

English edition


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 posted on GitHub looks 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'm attached to Hash # has_key?, And I still sometimes use Hash # has_key? In abandoned code.

You can continue to use Hash # has_key?, Which has not yet been officially declared deprecated (isn't it?), Or you should use other methods such asHash # key? Is it? My heart shakes.

If you really want to make it deprecated, I want you to give me guidance as soon as possible.

Recommended Posts

[Ruby] Hash # has_key? Is one step closer to the official deprecated?
[Java Servlet] The road of Senri is also one step to the first
How to retrieve the hash value in an array in Ruby
[Ruby] How to retrieve the contents of a double hash
[Ruby] I want to reverse the order of the hash table
Want to know what Ruby n is the power of 2? (Power judgment of 2)
About the behavior of ruby Hash # ==
[IOS] What to do when the image is filled with one color
[Ruby] I want to extract only the value of the hash and only the key
From Java9, the constructor of the class corresponding to primitive types is deprecated.
[Ruby] How to prevent errors when nil is included in the operation
When reassigning to an argument in a Ruby method and then calling `super` → The reassigned one is used