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