Ruby:如何在代码块中使用5个主要的Find方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ruby:如何在代码块中使用5个主要的Find方法相关的知识,希望对你有一定的参考价值。
This shows how we can find objects inside code blocks (aka data sets).
#find / detect returns an Object or nil, it will not find all the numbers puts " " + "#find returns an Object or nil" puts (1..10).find { |i| i == 5 } puts " " + "detect returns an Object or nil. Note:It only returns the first example." puts (1..10).detect { |i| i % 3 == 0 } puts " " + "detect can be used to determine which other numbers are within a range" puts (1..10).detect { |i| (1..10).include?(i * 3) } #find_all / select returns an Array puts " " + "find_all returns an Array" puts (1..10).find_all { |i| i % 3 == 0 } puts " " + "select returns an Array. Three numbers can be multiplied by 3 and still not exceed 10." puts (1..10).select { |i| (1..10).include?(i * 3) } # Method any? returns a Boolean puts " " + "any? returns a Boolean. In other words, are there any in the set that are true?" puts (1..10).any? { |i| i % 3 == 0 } # Method all? returns a Boolean puts " " + "all? returns a Boolean. Do all the conditions meet the requirement?" puts (1..10).all? { |i| i % 3 == 0 } # Method delete_if? returns an Array. puts " " + "delete_if anything is divisible by 3" puts [*1..10].delete_if { |i| i % 3 == 0 }
以上是关于Ruby:如何在代码块中使用5个主要的Find方法的主要内容,如果未能解决你的问题,请参考以下文章