Ruby | Hash fetch function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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 # declaring Hash value a = { "a" => 100, "b" => 200 } # declaring Hash value b = {"a" => 100} # declaring Hash value c = {"a" => 100, "c" => 300, "b" => 200} # Handling no key argument # fetch? Value puts "Hash a fetch form : #{a.fetch("x"){|el| "Not present, #{el}"}}\n\n" Output : Hash a fetch form : Not present, x Example #2 : Ruby # Ruby code for Hash.fetch() 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 Value puts "Hash a fetch form : #{a.fetch("a")}\n\n" puts "Hash b fetch form : #{b.fetch("a")}\n\n" puts "Hash c fetch form : #{c.fetch("b")}\n\n" Output : Hash a fetch form : 100 Hash b fetch form : 100 Hash c fetch form : 200 Comment More infoAdvertise with us Next Article Ruby | Hash fetch function K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby Hash-class Similar Reads 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 empty? function Hash#empty?() is a Hash class method which checks whether the Hash array has any key-value pair. Syntax: Hash.empty?()Parameter: Hash valuesReturn: true - if no key value pair otherwise return false Example #1 :  Ruby # Ruby code for Hash.empty?() method # declaring Hash value a = {a:100, b:200} # 1 min read Ruby | Hash fetch_values() function 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 Exam 1 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 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 delete() function delete() is an Hash class method which deletes the key-value pair and returns the value from hash whose key is equal to key. Syntax: Hash.delete() Parameter: Hash array Return: value from hash whose key is equal to deleted key. Example #1: Ruby # Ruby code for delete() method # declaring Hash value 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 otherwis 2 min read Ruby | Hash flatten() function Hash#flatten() is a Hash class method which returns the one-dimensional flattening hash array. Syntax: Hash.flatten() Parameter: Hash values Return: one-dimensional flattening hash array Example #1 : Ruby # Ruby code for Hash.flatten() method # declaring Hash value a = {a:100, b:200} # declaring Has 2 min read Ruby | Hash dig() function Hash#dig() is a Hash class method which finds the nested value which is specified by the sequence of the key object by calling dig at each step. Syntax: Hash.dig() Parameter: Hash values Return: nested value which is specified by the sequence of the key object by calling dig at each step. otherwise 2 min read Like