Ruby Hash rassoc()函数
rassoc()是一个Hash类方法,它通过Hash值搜索一个元素。
语法。Hash.rassoc()
参数:用于寻找元素的哈希值。
返回:通过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"]