`
minstrel
  • 浏览: 47474 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

ruby学习笔记--Blocks

阅读更多

Proc的两个使用方法

ruby 代码
  1. def some_mtd1 aproc     
  2.   aproc.call     
  3. end     
  4. some_mtd1 lambda { puts "aaaaa" }   
  5.  

 这个代码等同于下面这个代码段

ruby 代码
  1. def some_mtd2 &bproc        
  2.   bproc.call        
  3. end        
  4. some_mtd2 { puts "aaaaa" }     

 也等同于下面代码

ruby 代码
  1. ab = lambda { |x| puts x }      
  2. ab.call 'aaaaa'   

c = lambda { |i| puts i }
c = Proc.new { |i| puts i }
c = proc { |i| puts i } 

The above 3 statements do the same thing: instantiate a block object. ‘proc’ is an alias for ‘lambda‘ and they work slightly different than ‘Proc.new‘. In Ruby 1.9, ‘proc’ will probably be an alias for ‘Proc.new‘ instead.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics