Ruby Hash member? 函数

Ruby Hash member? 函数

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

语法。Hash.member?()

参数:哈希值

返回:true – 如果给定的键存在于哈希中,否则返回false。

例子 #1 :

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

输出:

Hash a member? form : false

Hash b member? form : false

Hash c member? form : false

例子 #2 :

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

输出:

Hash a member? form : true

Hash b member? form : false

Hash c member? form : true

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程