Ruby - Proc.new vs lambda

Introduction

Proc.new does not check that the number of arguments passed to the block matches the number of block parameters.

lambda does.

The behavior of the proc method is different in Ruby 1.8 and 1.9.

In Ruby 1.8, proc is equivalent to lambda-it checks the number of arguments.

In Ruby 1.9, proc is equivalent to Proc.new-it does not check the number of arguments:

a = Proc.new{|x,y,z| x = y*z; puts(x) }
a.call(2,5,10,100)        # This is not an error

b = lambda{|x,y,z| x = y*z; puts(x) }
b.call(2,5,10,100)        # This is an error

puts('---Block #2---' )
c = proc{|x,y,z| x = y*z; puts(x) }
c.call(2,5,10,100)        # This is an error in Ruby 1.8
                          # Not an error in Ruby 1.9

Related Topic