Ruby | Hash fetch_values() function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report Hash#fetch_values() is a Hash class method which returns array containing the values associated with the given keys. With no other arguments, it will raise a KeyError exception. Syntax: Hash.fetch_values() Parameter: Hash values Return: array containing the values associated with the given keys Example #1 : Ruby # Ruby code for Hash.fetch_values() method # declaring Hash value a = { "a" => 100, "b" => 200 } # declaring Hash value b = {"a" => 100} # block condition # fetch? Value puts "Hash a fetch_values form : #{a.fetch_values("x"){|k| k.upcase}}\n\n" Output : Hash a fetch_values form : ["X"] Example #2 : Ruby # Ruby code for Hash.fetch_values() 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} # fetch_values Value puts "Hash a fetch_values form : #{a.fetch_values("a")}\n\n" puts "Hash b fetch_values form : #{b.fetch_values("a")}\n\n" puts "Hash c fetch_values form : #{c.fetch_values("b")}\n\n" Output : Hash a fetch_values form : [100] Hash b fetch_values form : [100] Hash c fetch_values form : [200] Comment More infoAdvertise with us Next Article Ruby | Hash each() function K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby Hash-class Similar Reads Ruby | Hash values function Hash#values() is a Hash class method which returns the values present in the hash. Syntax: Hash.values() Parameter: Hash values Return: hash values Example #1 : Ruby # Ruby code for Hash.values() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, b:200} # decl 1 min read 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 has_value?() function Hash#has_value?() is a Hash class method which checks whether the given value is present in hash. Syntax: Hash.has_value?() Parameter: Hash values Return: true - if given value is present in hash otherwise return false Example #1 : Ruby # Ruby code for Hash.has_value?() method # declaring Hash value 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 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 Like