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.

评论
发表评论

您还没有登录,请登录后发表评论

minstrel
搜索本博客
最近加入圈子
存档
最新评论