📜  红宝石 |哈希 has_value?()函数

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

红宝石 |哈希 has_value?()函数

Hash#has_value?()是一个 Hash 类方法,用于检查给定值是否存在于哈希中。

示例 #1:

# Ruby code for Hash.has_value?() method
  
# declaring Hash value
a = {a:100, b:200}
  
# declaring Hash value
b = {a:100, c:300, b:200}
  
# declaring Hash value
c = {a:100}
  
  
# has_value? Value
puts "Hash a has_value? form : #{a.has_value?(200)}\n\n"
  
puts "Hash b has_value? form : #{b.has_value?(10)}\n\n"
  
puts "Hash c has_value? form : #{c.has_value?(100)}\n\n"

输出 :

Hash a has_value? form : true

Hash b has_value? form : false

Hash c has_value? form : true

示例 #2:

# Ruby code for Hash.has_value?() method
  
# declaring Hash value
a = { "a" => 100, "b" => 200 }
  
# declaring Hash value
b = {"a" => 100}
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
  
# has_value? Value
puts "Hash a has_value? form : #{a.has_value?(200)}\n\n"
  
puts "Hash b has_value? form : #{b.has_value?(10)}\n\n"
  
puts "Hash c has_value? form : #{c.has_value?(100)}\n\n"

输出 :

Hash a has_value? form : true

Hash b has_value? form : false

Hash c has_value? form : true