红宝石 |散列任何?()函数
any?()是一个 Hash 类方法,它检查模式的存在并将集合的每个元素传递给给定的块。
Syntax: Hash.any?()
Parameter: Hash for testing
Return: true – if the block ever returns a value other than false or nil otherwise return false
示例 #1:
# Ruby code for any?() 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}
# checking pattern
puts "pattern : #{a.any?()}\n\n"
puts "pattern : #{b.any?()}\n\n"
输出 :
pattern : true
pattern : true
示例 #2:
# Ruby code for any?() method
# declaring Hash value
c = {}
puts "pattern : #{c.any?}\n\n"
输出 :
pattern : false