any?()是一个Array类方法,该方法检查模式的存在并将集合中的每个元素传递给给定的块。
用法:Array.any?()
参数:测试阵列
返回:如果该块返回的值不是false或nil,则为true,否则返回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
相关用法
- Ruby Array dig()用法及代码示例
 - Ruby Array map!()用法及代码示例
 - Ruby Array at()用法及代码示例
 - Ruby Array combination()用法及代码示例
 - Ruby Array compact()用法及代码示例
 - Ruby Array compact!()用法及代码示例
 - Ruby Array concat()用法及代码示例
 - Ruby Array count()用法及代码示例
 - Ruby Array clear()用法及代码示例
 - Ruby Array collect!()用法及代码示例
 - Ruby Array size()用法及代码示例
 - Ruby Array bsearch()用法及代码示例
 - Ruby Array bsearch_index()用法及代码示例
 - Ruby Array rassoc()用法及代码示例
 - Ruby Array collect()用法及代码示例
 
注:本文由纯净天空筛选整理自mayank5326大神的英文原创作品 Ruby | Array any?() operation。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
