Skip to content
Related Articles

Related Articles

Ruby | Hash member? function
  • Last Updated : 07 Jan, 2020
GeeksforGeeks - Summer Carnival Banner

Hash#member?() is a Hash class method which checks whether the given key is present in hash is not.

Syntax: Hash.member?()

Parameter: Hash values

Return: true – if given key is present in hash otherwise return false

Example #1 :






# Ruby code for Hash.member?() method
  
# declaring Hash value
a = {a:100, b:200}
  
# declaring Hash value
b = {a:100, c:300, b:200}
  
# declaring Hash value
c = {a:100}
  
  
# member? Value
puts "Hash a member? form : #{a.member?("a")}\n\n"
  
puts "Hash b member? form : #{b.member?("c")}\n\n"
  
puts "Hash c member? form : #{c.member?("a")}\n\n"

Output :

Hash a member? form : false

Hash b member? form : false

Hash c member? form : false

Example #2 :




# Ruby code for Hash.member?() method
  
# declaring Hash value
a = { "a" => 100, "b" => 200 }
  
# declaring Hash value
b = {"a" => 100}
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
# member? Value
puts "Hash a member? form : #{a.member?("a")}\n\n"
  
puts "Hash b member? form : #{b.member?("c")}\n\n"
  
puts "Hash c member? form : #{c.member?("a")}\n\n"

Output :

Hash a member? form : true

Hash b member? form : false

Hash c member? form : true

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :