In Ruby, are there any differences between Proc.new { 'waffles' }
and proc { 'waffles' }
? I have found very few mentions of the second syntax.
From testing using irb
, I haven't found any obvious differences. Is the second syntactic sugar for the first?
A Proc object is an encapsulation of a block of code, which can be stored in a local variable, passed to a method or another Proc, and can be called. Proc is an essential concept in Ruby and a core of its functional programming features.
As opposed to a block, a proc is a Ruby object which can be stored in a variable and therefore reused many times throughout a program. # A proc is defined by calling Proc. new followed by a block. # When passing a proc to a method, an & is used to convert the proc into a block.
From Metaprogamming Ruby Page 113.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With