Mohit Jain Mohit Jain - 6 months ago 12
Ruby Question

How to check if a specific key is present in a hash or not?

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.

Answer

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

session.key?("user")
Comments