Ruby | Hash key?() function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report Hash#key?() is a Hash class method which checks whether the key corresponding to the value is present or not. Syntax: Hash.key?() Parameter: Hash values Return: true - if key corresponding to the value is present otherwise return false Example #1 : Ruby # Ruby code for Hash.key?() 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} # key? Value puts "Hash a key? form : #{a.key?("a")}\n\n" puts "Hash b key? form : #{b.key?("c")}\n\n" puts "Hash c key? form : #{c.key?("a")}\n\n" Output : Hash a key? form : true Hash b key? form : false Hash c key? form : false Example #2 : Ruby # Ruby code for Hash.key?() 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} # key? Value puts "Hash a key? form : #{a.key?("a")}\n\n" puts "Hash b key? form : #{b.key?("c")}\n\n" puts "Hash c key? form : #{c.key?("a")}\n\n" Output : Hash a key? form : true Hash b key? form : false Hash c key? form : true Comment More infoAdvertise with us Next Article Ruby | Hash key?() function K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby Hash-class Similar Reads Ruby | Hash key() function Hash#key() is a Hash class method which gives the key value corresponding to the value. If value doesn't exist then return nil. Syntax: Hash.key() Parameter: Hash values Return: key corresponding to the value nil - If value doesn't exist Example #1 : Ruby # Ruby code for Hash.key() method # declarin 2 min read Ruby | Hash keys() function Hash#keys() is a Hash class method which gives an array with all the keys present in the hash. Syntax: Hash.keys() Parameter: Hash values Return: array with all the keys present in the hash Example #1 : Ruby # Ruby code for Hash.keys() method # declaring Hash value a = {a:100, b:200} # declaring Has 2 min read Ruby | Hash has_key?() function Hash#has_key?() is a Hash class method which checks whether the given key is present in hash. Syntax: Hash.has_key?() Parameter: Hash values Return: true - if the key is present otherwise return false Example #1 : Ruby # Ruby code for Hash.has_key?() method # declaring Hash value a = {a:100, b:200} 2 min read Ruby | Hash each_key function Hash#each_key() is a Hash class method which finds the nested value which calls block once for each_key pair in the hash by passing the key as parameters. Syntax: Hash.each_key() Parameter: Hash values Return: calls block once for key_value pair in hash with key as a parameter otherwise, Enumerator 2 min read Ruby | Hash each_key() function Hash#each_key() is a Hash class method which finds the nested value which calls block once for each_key key in hash by passing the key pair as parameters. Syntax: Hash.each_key() Parameter: Hash values Return: calls block once for each_key key in hash with key as parameter otherwise Enumerator if no 2 min read Like