DEV Community

Subash
Subash

Posted on

Ruby Lambda VS Proc

Lambda and Proc - both are container for a block of code. Define a set of codes inside either of them and save them as a variable for later use.

Let's see the usage of both in detail

Lambda

A lambda is very similar to method, just the syntax varies. Below is the syntax for creation

test_lambda = -> { puts 'something'}

# lambda using arguments
test_lambda = -> (x) { x + 1}
Enter fullscreen mode Exit fullscreen mode

Now we have defined a lambda and store in a variable test_lambda, we have to call it to use. There are multiple ways to call a lambda. Below is the list of ways

test_lambda.call
test_lambda.call()
test_lambda.()
test_lambda.[]
test_lambda.[]
test_lambda.===
Enter fullscreen mode Exit fullscreen mode

I prefer .call() as it is more readable than others

If we call the defined lambda, the block of code is executed and result is displayed

test_lambda.call(5)
6
=> 6
Enter fullscreen mode Exit fullscreen mode

As I stated earlier, lambda works very similar to methods if lambda is defined with args, the number of args should match during call, else it will raise an exception just like a regular method.

Also like a regular method a lambda will return normally to it's __callee__ rather than returning from a context

Example

def demo_lambda(num)
  test_lambda = -> (x) { return x + 1 }
  new_value = test_lambda.call(num)
  puts 'new value returned'
  new_value
end
Enter fullscreen mode Exit fullscreen mode

Here the return of the test_lambda will return the value within the scope of the lambda (i.e returned value is stored in new_value) and will proceed with the execution of below lines

Proc

Procs are similar to lambda, but there are few differnces and the choice to use between Proc and Lambda depends on it

Proc creation uses different syntax

test_proc = Proc.new {|x| x+1 }
Enter fullscreen mode Exit fullscreen mode

Calling Proc is similar to lambda. It can be called using .call(), .(), .[], .===

Proc doesn't care about the number of arguments passed. But if the argument is used for any kind of calculation or manipulation it will raise an error.

Example

p = Proc.new{|x| puts 'something'}
p.call()
=> "Something"

p = Proc.new{|x| x*2 }
p.call()
(irb):61:in `block in <main>': undefined method `*' for nil:NilClass (NoMethodError)

p = Proc.new{|x| x*2 }

#since there is a calculation based on the args passed NilClass error is thrown
Enter fullscreen mode Exit fullscreen mode

One more major difference is proc will return from the current context. i.e if proc is inside an method, then calling return from proc is equivalent to returning from that method. Let's see an example

def demo_proc(num)
  test_proc = Proc.new{ |x| return x + 1 }
  new_value = test_proc.call(num)
  puts 'new value returned'
  new_value
end

demo_proc(5)

irb(main):008:0> demo_proc(5)
=> 6
Enter fullscreen mode Exit fullscreen mode

In the above sample, the puts statement will not be printed. This is because the return in test_proc will return from the demo_proc itself.

Source: Proc Lambda

Top comments (0)