我正在阅读《Ruby程序设计》第1.9版,作者这样使用lambda:
bo = lambda {|param| puts "You called me with #{param}"}
bo.call 99 => 'You called me with 99'
bo.call "cat" => 'You called me with cat'
我的问题是:这样做有什么优劣之处呢?和定义一个执行同样操作的方法有何不同呢?就像这样:
def bo(param)
puts "You called me with #{param}"
end
bo("hello") => 'You called me with hello'
对我而言,lambda语法看起来更加混乱和像意大利面一样交织在一起。