Ruby | Hash each() function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 : Ruby # Ruby code for Hash.each() 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 form : #{a.each()}\n\n" puts "Hash b each form : #{b.each {|key| puts "#{key}"}}\n\n" puts "Hash c each form : #{c.each {|value| puts "#{value}"}}\n\n" Output : Hash a each form : # [:a, 100] [:c, 300] [:b, 200] Hash b each form : {:a=>100, :c=>300, :b=>200} [:a, 100] Hash c each form : {:a=>100} Example #2 : Ruby # Ruby code for Hash.each() 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 form : #{a.each()}\n\n" puts "Hash b each form : #{b.each {|key| puts "#{key}"}}\n\n" puts "Hash c each form : #{c.each {|value| puts "#{value}"}}\n\n" Output : Hash a each form : # ["a", 100] Hash b each form : {"a"=>100} ["a", 100] ["c", 300] ["b", 200] Hash c each 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 fetch function Hash#fetch() is a Hash class method which returns a value from the hash for the given key. With no other arguments, it will raise a KeyError exception. Syntax: Hash.fetch() Parameter: Hash values Return: value from the hash for the given key Example #1 : Ruby # Ruby code for Hash.fetch() method # de 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 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_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 otherwis 2 min read Ruby | Hash eql? function Hash#eql?() is a Hash class method which checks whether the two Hash arrays are equal or not. Syntax: Hash.eql?() Parameter: Hash values Return: true - if two hash arrays are equal otherwise return false Example #1 : Ruby # Ruby code for Hash.eql?() method # declaring Hash value a = {a:100, b:200} # 2 min read Ruby | Hash assoc() function assoc() is an Hash class method which searches an element through the Hash. Syntax: Hash.assoc() Parameter: Hashs for finding elements. Return: searches an element through the Hash Example #1: Ruby # Ruby code for assoc() method # declaring Hash value a = { "a" => 100, "b" = 1 min read Like