红宝石 |哈希包含?()函数
Hash#include?()是一个 Hash 类方法,用于检查给定键是否存在于散列中。
Syntax: Hash.include?()
Parameter: Hash values
Return: true – if given key”” 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.include?("a")}\n\n"
puts "Hash b has_value? form : #{b.has_value?("c")}\n\n"
puts "Hash c has_value? form : #{c.has_value?("c")}\n\n"
输出 :
Hash a has_value? form : false
Hash b has_value? form : false
Hash c has_value? form : false
示例 #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.include?("a")}\n\n"
puts "Hash b has_value? form : #{b.has_value?("c")}\n\n"
puts "Hash c has_value? form : #{c.has_value?("c")}\n\n"
输出 :
Hash a has_value? form : true
Hash b has_value? form : false
Hash c has_value? form : false