Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Proc.new { 'waffles' } vs. 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.

From testing using irb, I haven't found any obvious differences. Is the second syntactic sugar for the first?

like image 739
ClosureCowboy Avatar asked Jan 17 '11 06:01

ClosureCowboy


People also ask

What is Proc new Ruby?

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.

What is the use of Proc in Ruby?

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.


1 Answers

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.

like image 188
Prajna Avatar answered Sep 29 '22 11:09

Prajna