Ruby:Proc.new {'waffles'} 与 proc {'waffles'} [英] Ruby: Proc.new { 'waffles' } vs. proc { 'waffles' }
问题描述
在 Ruby 中,Proc.new { 'waffles' }
和 proc { 'waffles' }
之间有什么区别吗?我发现很少有人提到第二种语法.
In Ruby, are there any differences between Proc.new { 'waffles' }
and proc { 'waffles' }
? I have found very few mentions of the second syntax.
从使用 irb
进行的测试来看,我没有发现任何明显的差异.第二种语法糖是第一种吗?
From testing using irb
, I haven't found any obvious differences. Is the second syntactic sugar for the first?
推荐答案
来自 Metaprogamming Ruby 第 113 页.
From Metaprogamming Ruby Page 113.
在 Ruby 1.8 中,Kernel#proc() 实际上是 Kernel#lambda() 的同义词.由于程序员的强烈抗议,Ruby 1.9 将 proc() 改为 Proc.new() 的同义词.
In Ruby 1.8, Kernel#proc() is actually a synonym for Kernel#lambda(). Because of loud protest from programmers, Ruby 1.9 made proc() a synonym for Proc.new() instead.
这篇关于Ruby:Proc.new {'waffles'} 与 proc {'waffles'}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!