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

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

红宝石 |哈希 rassoc()函数

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

示例 #1:

# Ruby code for rassoc() method
  
# declaring Hash value
a = { "a" => "abc", "b" => "200" }
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
puts "check : #{a.rassoc("200")}\n\n"
  
puts "check : #{c.rassoc(300)}\n\n"
  
puts "check : #{c.rassoc(100)}\n\n"

输出 :

check : ["b", "200"]

check : ["c", 300]

check : ["a", 100]

示例 #2:

# Ruby code for rassoc() method
  
# declaring Hash value
a = { "a" => "geek", "b" => "200" }
  
# declaring Hash value
c = {"a" => "abc", "c" => 300, "b" => 200}
  
puts "check : #{a.rassoc("geek")}\n\n"
  
puts "check : #{c.rassoc(300)}\n\n"
  
puts "check : #{c.rassoc("abc")}\n\n"

输出 :

check : ["a", "geek"]

check : ["c", 300]

check : ["a", "abc"]