Ruby | Hash assoc() function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report 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" => 200 } # declaring Hash value c = {"a" => 100, "c" => 300, "b" => 200} puts "check : #{a.assoc("a")}\n\n" puts "check : #{c.assoc("b")}\n\n" puts "check : #{c.assoc("c")}\n\n" Output : check : ["a", 100] check : ["b", 200] check : ["c", 300] Example #2: Ruby # Ruby code for assoc() method # declaring Hash value b = {a:100, c:300, "b" => ["200", "100", "300"]} puts "check : #{b.assoc("b")}\n\n" Output : check : ["b", ["200", "100", "300"]] Comment More infoAdvertise with us Next Article Ruby | Hash assoc() 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 any?() function any?() is a Hash class method which checks for the presence of a pattern and passes each element of the collection to the given block. Syntax: Hash.any?() Parameter: Hash for testing Return: true - if the block ever returns a value other than false or nil otherwise return false Example #1: Ruby # Ru 1 min read Ruby | Hash compact() function compact () is a Hash class method which returns the Hash after removing all the 'nil' value elements (if any) from the Hash. Syntax: Hash.compact() Parameter: Hash to remove the 'nil' value from. Return: removes all the nil values from the Hash. Example #1: Ruby # Ruby code for compact() method # sh 2 min read Ruby | Hash compact!() function compact! () is a Hash class method which returns the Hash after removing all the 'nil' value elements (if any) from the Hash. If there are no nil values in the Hash it returns back the nil value. Syntax: Hash.compact!() Parameter: Hash to remove the 'nil' value from. Return: removes all the nil valu 2 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 Like