HiveBrain v1.2.0
Get Started
← Back to all entries
snippetrubyCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
keyhowcheckhashnotspecificpresent

Problem

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.

Solution

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

session.key?("user")

Code Snippets

session.key?("user")

Context

Stack Overflow Q#4528506, score: 1145

Revisions (0)

No revisions yet.