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

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

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

Hash#key?()是一个 Hash 类方法,用于检查与值对应的键是否存在。

示例 #1:

# Ruby code for Hash.key?() 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}
   
   
# key? Value
puts "Hash a key? form : #{a.key?("a")}\n\n"
   
puts "Hash b key? form : #{b.key?("c")}\n\n"
   
puts "Hash c key? form : #{c.key?("a")}\n\n"

输出 :

Hash a key? form : true

Hash b key? form : false

Hash c key? form : false

示例 #2:

# Ruby code for Hash.key?() 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}
  
# key? Value
puts "Hash a key? form : #{a.key?("a")}\n\n"
  
puts "Hash b key? form : #{b.key?("c")}\n\n"
  
puts "Hash c key? form : #{c.key?("a")}\n\n"

输出 :

Hash a key? form : true

Hash b key? form : false

Hash c key? form : true