ruby

Detect returns the first item in the list for which the block returns TRUE. Your first example:

[1,2,3,4,5,6,7].detect { |x| x.between?(3,4) }
=> 3
Returns 3 because that is the first item in the list that returns TRUE for the expression x.between?(3,4).

detect stops iterating after the condition returns true for the first time. select will iterate until the end of the input list is reached and returns all of the items where the block returned true.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1《少有人走的路-勇敢的面对谎言》第四章开篇的扉页上有这样一句引导语,夫妻之间真正的爱,不是树缠藤,也不是藤缠树,...
    北苏阅读 774评论 0 0
  • 文/ Kurny 一面二十世纪的墙 面向二十一世纪 开了一道口子 遗留半扇窗 斑驳了岁月 另一半不知所踪 闭合着 ...
    Kurny91阅读 262评论 1 2
  • vhjhhuisnwbbjjsjbbdbjsjj
    仇志轩阅读 121评论 0 5