Open In App

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"]]

Next Article

Similar Reads