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
相关用法
- Ruby Hash dig()用法及代码示例
- Ruby Hash each()用法及代码示例
- Ruby Hash eql?用法及代码示例
- Ruby Hash any?()用法及代码示例
- Ruby Hash key()用法及代码示例
- Ruby Hash key?()用法及代码示例
- Ruby Set empty?()用法及代码示例
- Ruby Hash delete()用法及代码示例
- Ruby Hash delete_if()用法及代码示例
- Ruby Hash values用法及代码示例
- Ruby Hash each_key用法及代码示例
- Ruby Hash fetch用法及代码示例
- Ruby Hash fetch_values()用法及代码示例
- Ruby Hash flatten()用法及代码示例
- Ruby Hash rassoc()用法及代码示例
注:本文由纯净天空筛选整理自Kirti_Mangal大神的英文原创作品 Ruby | Hash empty? function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。