Open In App

Ruby | Hash member? function

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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



Similar Reads

Ruby | Set member?() function
The member?() is an inbuilt method in Ruby returns true if the set contains the given object. It returns false if it does not contains the given object. Syntax: s1_name.member?(object) Parameters: The function accepts a mandatory parameter object whose presence is to be checked for. Return Value: It returns true if the set contains the given object
1 min read
Ruby | Enumerable member? function
The member?() of enumerable is an inbuilt method in Ruby returns a boolean value. It returns true if the enumerable contains the given elements, or else it returns false. Syntax: enu.member?(el) Parameters: The function takes an element which is to be checked for. Return Value: It returns a boolean value. Example #1: # Ruby program for member? meth
1 min read
Ruby | Range member? function
The member?() is an inbuilt method in Ruby returns a boolean value true if the given object lies within the given range, else it returns false. Syntax: range1.member?(obj) Parameters: The function accepts an object which is to be checked for. Return Value: It returns a boolean value true if the given object lies within the given range, else it retu
1 min read
Ruby | Hash each_pair() function
Hash#each_pair() is a Hash class method which finds the nested value which calls block once for each pair in hash by passing the key_value pair as parameters. Syntax: Hash.each_pair() Parameter: Hash values Return: calls block once for key_value pair in hash with key_value pair as parameter otherwise Enumerator if no argument is passed. Example #1
2 min read
Ruby | Hash to_s() function
Hash#to_s() is a Hash class method which gives the string representation of hash. Syntax: Hash.to_s() Parameter: Hash values Return: string representation of hash Example #1 : # Ruby code for Hash.to_s() 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} # to_s Value
1 min read
Ruby | Hash invert() function
Hash#invert() is a Hash class method which gives the hash by reverting keys to values and values to key. Syntax: Hash.invert() Parameter: Hash values Return: hash by reverting keys to values and values to key Example #1 : # Ruby code for Hash.invert() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, b:200}
2 min read
Ruby | Hash include?() function
Hash#include?() is a Hash class method which checks whether the given key is present in hash. Syntax: Hash.include?() Parameter: Hash values Return: true - if given key"" is present in hash otherwise return false Example #1 : # Ruby code for Hash.has_value?() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300,
2 min read
Ruby | Hash inspect() function
Hash#inspect() is a Hash class method which gives the string representation of hash. Syntax: Hash.inspect()Parameter: Hash valuesReturn: string representation of the hash Example #1 : C/C++ Code # Ruby code for Hash.inspect() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, b:200} # declaring Hash value c =
1 min read
Ruby | Array class hash() function
hash() is an Array class method which returns the hash code of the array elements Syntax: Array.hash() Parameter: Array Return: hash code of the array elements Example #1 : # Ruby code for hash() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, nil, nil, 50, 6] # hash puts
1 min read
Ruby | Hash rassoc() function
rassoc() is an Hash class method which searches an element through the Hash value. Syntax: Hash.rassoc() Parameter: Hashs for finding elements. Return: searches an element through the Hash value Example #1: # Ruby code for rassoc() method # declaring Hash value a = { "a" => "abc", "b" => "200" } # decla
1 min read