RajG
RajG

Reputation: 832

Hash Key in Ruby?

I am trying to initialise the hash pair and then look for the key using has.key in the hash pair value. I have given the condition see below:

if seen.has.key?(var)
    <Execute Condition>

Could you shed lights on it how this actually works? I have tried looking around but still confuses me. Thanks

Upvotes: 0

Views: 147

Answers (1)

sepp2k
sepp2k

Reputation: 370445

seen={} assigns an empty hashmap to the variable seen and has.key calls the method key on the object has. Assuming that has is a hashmap, key is a method that takes a value as its argument and returns the key that maps to the given value in the hashmap.

seen.has.key?(var) calls the has method on seen and then calls the key? method with the argument var on the result of that. Since hashmap objects don't have a has method, this will cause a NoMethodError.

Upvotes: 7

Related Questions