Ruby Hash empty?函数

Ruby Hash empty?函数

Hash#empty?() 是一个Hash类方法,用于检查Hash数组中是否有键值对。

语法: Hash.empty?()

参数: Hash值

返回: 真 – 如果没有键值对,否则返回假

例子 #1 :

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

输出 :

Hash a empty? form : false

Hash b empty? form : false

Hash c empty? form : false

例子#2:

# Ruby code for Hash.empty?() method
  
# declaring Hash value
a = { "a" => 100, "b" => 200 }
  
# declaring Hash value
b = {}
  
# declaring Hash value
c = {"a" => 100, "c" => 300, "b" => 200}
  
# emoty? Value
puts "Hash a empty? form : #{a.empty?()}\n\n"
  
puts "Hash b empty? form : #{b.empty?()}\n\n"
  
puts "Hash c empty? form : #{c.empty?()}\n\n"

输出:

Hash a empty? form : false

Hash b empty? form : true

Hash c empty? form : false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程