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 code for assoc() method # declaring Hash valuea = { "a" => 100, "b" => 200 } # declaring Hash valuec = {"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 code for assoc() method # declaring Hash valueb = {a:100, c:300, "b" => ["200", "100", "300"]} puts "check : #{b.assoc("b")}\n\n" |
Output :
check : ["b", ["200", "100", "300"]]


