红宝石 |数组 any?() 操作
any?()是一个 Array 类方法,它检查是否存在模式并将集合的每个元素传递给给定的块。
Syntax: Array.any?()
Parameter: array 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
# checking pattern
puts "pattern : #{%w[geeks or geeks].any? { |word| word.length <= 3 }}\n\n"
puts "pattern : #{%w[dot grow cat].any? { |word| word.length >= 4 }}\n\n"
输出 :
pattern : true
pattern : true
解释 :
In above code two conditions are giving i.e.
– word_length should be equal or less than 3 and
– word_length should be equal or greater than 4
If the pattern follows it (as in the code), it results yes otherwise false
示例 #2:
# Ruby code for any?() method
# checking pattern
puts "pattern : #{%w[geeks or geeks].any?()}\n\n"
puts "pattern : #{[].any?}\n\n"
输出 :
pattern : true
pattern : false
解释 :
In this code
part a) – true as it returns a pattern
part b) – false as it returns nil pattern