Is there any difference between the two methods for the Ruby Hash, or is it just "there is more than 1 way to do it"?
I could not see any measureable difference between the two.
Thanks,
2 Answers
According to the Ruby source, has_key?, key?, include? and member? are the same implementation.
Comments
To see the method definition's source code see documentation, find the method you're looking for then click on the method to expand to see the actual source code:
https://ruby-doc.org/core-2.5.0/Hash.html#method-i-member-3F
rb_hash_has_key(VALUE hash, VALUE key)
{
if (!RHASH(hash)->ntbl)
return Qfalse;
if (st_lookup(RHASH(hash)->ntbl, key, 0)) {
return Qtrue;
}
return Qfalse;
}
https://ruby-doc.org/core-2.5.0/Hash.html#method-i-has_key-3F
rb_hash_has_key(VALUE hash, VALUE key)
{
if (!RHASH(hash)->ntbl)
return Qfalse;
if (st_lookup(RHASH(hash)->ntbl, key, 0)) {
return Qtrue;
}
return Qfalse;
}
Hash.instance_method(:has_key?) == Hash.instance_method(:member?) #=> true