4

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

2 Answers 2

11

According to the Ruby source, has_key?, key?, include? and member? are the same implementation.

Sign up to request clarification or add additional context in comments.

Comments

3

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;
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.