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.


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 ={|ele| puts ele*10}
lmbda = lambda {|ele| puts ele*10}



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 = do |ele,ele1,ele2,ele3| 
	puts ele+ele1+ele2*ele3

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




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 }
  p "Welcome to from lambda"

def method2
  x = { return }
  p "Welcome to from Procs"



"Welcome to 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!


Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.