Home » Ruby programming

Differences between Lambdas and Procs in Ruby

Ruby Lambdas vs Procs: Here, we are going to learn about the differences between Lambdas and Procs in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on November 21, 2019

We know how to declare a lambda and proc in Ruby? Their implementation is almost the same and they both are used for the same purpose. For a quick revision, Procs and Lambdas are used to store methods or set of logics inside a variable. You can call that variable inside your code wherever you want. But there exist some differences between them. Let us see how they differ from each other.

Declaration:

Procs are required to be declared with the help of ".new" method whereas you can declare a lambda just by writing the keyword "lambda". Refer the example given below and analyze the difference,

prc = Proc.new{|ele| puts ele*10}
lmbda = lambda {|ele| puts ele*10}
prc.call(12)
lmbda.call(34)

Output

120
340

Argument count

They both differ in the behavior of argument count as well. Procs do not count arguments whereas lambda does which means that if you pass the wrong number of arguments inside the call method of lambda then you will have to face an error whereas if you do the same in case of procs you will not face any kind of Exceptional situation. Refer the example given below and analyze the code,

prc = Proc.new do |ele,ele1,ele2,ele3| 
	puts ele+ele1+ele2*ele3
end

lmbda = lambda do|ele,ele1,ele2,ele3|
    puts ele*10+ele2+ele1+ele3
end

prc.call(12,11,22,23,66,77)
lmbda.call(34,34,2,4)

Output

529
380

Explanation:

In the above code you can observe that in the call method of proc we are passing the wrong number of arguments or you can say that the number of arguments is more than expected by the proc but still interpreter is not giving any kind of error but if we do the same in the case of lambda we will have to face an error named as "the wrong number of arguments (given 5, expected 4): Argument error".

Return behavior

This is the major difference you will find between procs and lambdas. Procs behave in a way that it will get explicitly out from the code when it will find any return statement or you can say that the further processing will be stopped once it finds the return keyword whereas this is not the case of lambdas it first completes all the processing then it gets out from the block. Refer to the code given below and analyze the difference.

def method1
  x = lambda { return }
  x.call
  p "Welcome to Includehelp.com from lambda"
end

def method2
  x = Proc.new { return }
  x.call
  p "Welcome to Includehelp.com from Procs"
end

method1
method2

Output

"Welcome to Includehelp.com from lambda"

In the output, you can observe that the string from lambda is getting printed only because proc has found the return statement and it got exit leaving behind the unprocessed statements.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.