Skip to content
Related Articles

Related Articles

Improve Article

Ruby | Hash assoc() function

  • Last Updated : 07 Jan, 2020

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"

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"

Output :

check : ["b", ["200", "100", "300"]]
My Personal Notes arrow_drop_up
Recommended Articles
Page :