proc和lambda最主要的区别就是可以把lambda看作和定义一个方法的行为是一致的,而proc是一个真正意义上的代码块。< class="ttyprint">a = proc { |x| x }p a.call b = lambda { |x| x }p b.call(2) >< class="ttyprint">def hi a = proc { return 3 } a.call 4enddef hello a = lambda { return 5 } a.call 6endp hi => 3 p hello => 6 >< class="ttyprint">def hi x xendp method(:hi).call(7) puts "*" * 50def bar_1 &block block.callenddef bar_2 yieldendrequire 'benchmark'puts 'bar_1:'p Benchmark.realtime { 1_000_000.times do bar_1 { 'ok' } end}puts 'bar_2:'p Benchmark.realtime { 1_000_000.times do bar_2 { 'ok' } end} >
|