(1..10).detect(lambda{ "none" }){|i| i == 11} #=> "none"
为什么我们需要lambda?为什么我们不传递默认值本身,因为(在我的测试中),lambda不能有任何参数?喜欢这个:
(1..10).detect("none"){|i| i == 11} #=> "none"
解决方法
Everyone has an individual background. Someone may come from Python,someone else may come from Perl,and they may be surprised by different aspects of the language. Then they come up to me and say,‘I was surprised by this feature of the language,so Ruby violates the principle of least surprise.’ Wait. Wait. The principle of least surprise is not for you only. The principle of least surprise means principle of least my surprise. And it means the principle of least surprise after you learn Ruby very well. For example,I was a C++ programmer before I started designing Ruby. I programmed in C++ exclusively for two or three years. And after two years of C++ programming,it still surprises me.
所以这里的理性真的是任何人的猜测.
一种可能性是它允许或与您想要有条件地运行昂贵的用例一致:
arr.detect(lambda { do_something_expensive }) { |i| is_i_ok? i }
或者由@majioa暗示,也许传递一个方法:
arr.detect(method(:some_method)) { |i| is_i_ok? i }