📜  红宝石 |哈希 assoc()函数

📅  最后修改于: 2022-05-13 01:54:45.529000             🧑  作者: Mango

红宝石 |哈希 assoc()函数

assoc()是一个 Hash 类方法,它通过 Hash 搜索元素。

示例 #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"

输出 :

check : ["a", 100]

check : ["b", 200]

check : ["c", 300]

示例 #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"

输出 :

check : ["b", ["200", "100", "300"]]