當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Array any?()用法及代碼示例


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



相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Array any?() operation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。