当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Hash empty?用法及代码示例


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

用法:Hash.empty?()

参数:散列值


返回:true-如果没有键值对,否则返回false

范例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



相关用法


注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 Ruby | Hash empty? function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。