Ruby Hash include?()函数

Ruby Hash include?()函数

Hash#include?()是一个Hash类方法,用于检查给定的键是否存在于Hash中。

语法。Hash.include?()

参数:哈希值

返回:true – 如果给定的键存在于哈希中,否则返回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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程