红宝石 |哈希 has_value?()函数
Hash#has_value?()是一个 Hash 类方法,用于检查给定值是否存在于哈希中。
Syntax: Hash.has_value?()
Parameter: Hash values
Return: true – if given value is present in hash otherwise return false
示例 #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