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 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" |
chevron_right
filter_none
Output :
check : ["a", 100] check : ["b", 200] check : ["c", 300]
Example #2:
# Ruby code for assoc() method # declaring Hash value b = {a:100, c:300, "b" => ["200", "100", "300"]} puts "check : #{b.assoc("b")}\n\n" |
chevron_right
filter_none
Output :
check : ["b", ["200", "100", "300"]]
Recommended Posts:
- Ruby | Array assoc() function
- Ruby | Hash eql? function
- Ruby | Hash any?() function
- Ruby | Hash key() function
- Ruby | Hash each() function
- Ruby | Hash dig() function
- Ruby | Hash key?() function
- Ruby | Hash delete_if() function
- Ruby | Hash rassoc() function
- Ruby | Hash merge function
- Ruby | Hash member? function
- Ruby | Hash fetch function
- Ruby | Hash compact!() function
- Ruby | Hash compare_by_identity? () function
- Ruby | Hash delete() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



