750

I want to check whether the "user" key is present or not in the session hash. How can I do this?

Note that I don't want to check whether the key's value is nil or not. I just want to check whether the "user" key is present.

Eden Landau
  • 553
  • 4
  • 12
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274

8 Answers8

1124

Hash's key? method tells you whether a given key is present or not.

session.key?("user")
sepp2k
  • 363,768
  • 54
  • 674
  • 675
341

While Hash#has_key? gets the job done, as Matz notes here, it has been deprecated in favour of Hash#key?.

hash.key?(some_key)
Andre Figueiredo
  • 12,930
  • 8
  • 48
  • 74
Bozhidar Batsov
  • 55,802
  • 13
  • 100
  • 117
55

Hash instance has a key? method:

{a: 1}.key?(:a)
=> true

Be sure to use the symbol key or a string key depending on what you have in your hash:

{'a' => 2}.key?(:a)
=> false
installero
  • 9,096
  • 3
  • 39
  • 43
  • 3
    Apprently existed as early as Ruby 1.6: http://ruby-doc.com/docs/ProgrammingRuby/html/ref_c_hash.html#Hash.key_qm (perhaps earlier, dunno where to check documented) – Beni Cherniavsky-Paskin Jun 28 '16 at 14:36
37

It is very late but preferably symbols should be used as key:

my_hash = {}
my_hash[:my_key] = 'value'

my_hash.has_key?("my_key")
 => false 
my_hash.has_key?("my_key".to_sym)
 => true 

my_hash2 = {}
my_hash2['my_key'] = 'value'

my_hash2.has_key?("my_key")
 => true 
my_hash2.has_key?("my_key".to_sym)
 => false 

But when creating hash if you pass string as key then it will search for the string in keys.

But when creating hash you pass symbol as key then has_key? will search the keys by using symbol.


If you are using Rails, you can use Hash#with_indifferent_access to avoid this; both hash[:my_key] and hash["my_key"] will point to the same record

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Abdul Baig
  • 3,683
  • 3
  • 21
  • 48
  • 7
    If you are using Rails, you can use `Hash#with_indifferent_access` to avoid this; both `:my_key` and `"my_key"` will point to the same record – Devon Parsons Apr 06 '16 at 14:07
  • This is the best answer especially when you are looking for a key coming from a variable, e.g., `my_hash.has_key? my_key.to_sym` :) – q9f Dec 16 '21 at 18:44
9

Another way is here

hash = {one: 1, two: 2}

hash.member?(:one)
#=> true

hash.member?(:five)
#=> false
Arvind singh
  • 1,312
  • 15
  • 15
5

You can always use Hash#key? to check if the key is present in a hash or not.

If not it will return you false

hash =  { one: 1, two:2 }

hash.key?(:one)
#=> true

hash.key?(:four)
#=> false
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
4

In Rails 5, the has_key? method checks if key exists in hash. The syntax to use it is:

YourHash.has_key? :yourkey
Ahmad MOUSSA
  • 2,729
  • 19
  • 31
0
h = {}
if h.respond_to?(:key?) && h.key?('text') #with respond_to method check if it's hash
   puts h['text'] 
end
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Feb 03 '23 at 00:18