Ruby | Hash each_key function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 if no argument is passed. Example #1 : Ruby # Ruby code for Hash.each_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} # each Value puts "Hash a each_key form : #{a.each_key()}\n\n" puts "Hash b each_key form : #{b.each_key {|key| puts "#{key}"}}\n\n" puts "Hash c each_key form : #{c.each_key {|value| puts "#{value}"}}\n\n" Output : Hash a each_key form : # a c b Hash b each_key form : {:a=>100, :c=>300, :b=>200} a Hash c each_key form : {:a=>100} Example #2 : Ruby # Ruby code for Hash.each_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} # each Value puts "Hash a each_key form : #{a.each_key()}\n\n" puts "Hash b each_key form : #{b.each_key {|key| puts "#{key}"}}\n\n" puts "Hash c each_key form : #{c.each_key {|value| puts "#{value}"}}\n\n" Output : Hash a each_key form : # a Hash b each_key form : {"a"=>100} a c b Hash c each_key form : {"a"=>100, "c"=>300, "b"=>200} Comment More infoAdvertise with us Next Article Ruby | Hash each_key function K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby Hash-class Similar Reads 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 Ruby | Hash each() function Hash#each() is a Hash class method which finds the nested value which calls block once for each key in hash by passing the key-value pair as parameters. Syntax: Hash.each() Parameter: Hash values Return: calls block once for each key in hash otherwise Enumerator if no argument is passed. Example #1 2 min read 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 key?() function 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 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 Like