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

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

红宝石 |哈希包含?()函数

Hash#include?()是一个 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.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