javaee论坛

普通会员

225648

帖子

345

回复

359

积分

楼主
发表于 2017-06-21 04:54:31 | 查看: 75 | 回复: 0
proc和lambda最主要的区别就是可以把lambda看作和定义一个方法的行为是一致的,而proc是一个真正意义上的代码块。

< class="ttyprint">## case 1a = proc { |x| x }p a.call # => nilb = lambda { |x| x }p b.call(2) # => exception if none< class="ttyprint">## case 2def hi a = proc { return 3 } a.call 4enddef hello a = lambda { return 5 } a.call 6endp hi => 3 p hello => 6 < class="ttyprint">## case 3def hi x xendp method(:hi).call(7) # to lambdaputs "*" * 50## case lastdef 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}

您需要登录后才可以回帖 登录 | 立即注册

触屏版| 电脑版

技术支持 历史网 V2.0 © 2016-2017