Ruby 哈希 value?()函数
Hash#value?() 是一个Hash类方法,用于检查参数’value’是否存在于数组中。
语法: Hash.value?()
参数: Hash value?
返回: true – 如果参数’value’存在于数组中,否则返回false。
例子 #1 :
# Ruby code for Hash.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}
# value? Value
puts "Hash a value? form : #{a.value?(200)}\n\n"
puts "Hash b value? form : #{b.value?(100)}\n\n"
puts "Hash c value? form : #{c.value?(300)}\n\n"
输出:
Hash a value? form : true
Hash b value? form : true
Hash c value? form : false
例子 #2 :
# Ruby code for Hash.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}
# value? Value
puts "Hash a value? form : #{a.value?(200)}\n\n"
puts "Hash b value? form : #{b.value?(200)}\n\n"
puts "Hash c value? form : #{c.value?(300)}\n\n"
输出:
Hash a value? form : true
Hash b value? form : false
Hash c value? form : true