DEV Community

Cover image for Sharpen your Ruby: Procs and Lambdas

Sharpen your Ruby: Procs and Lambdas

ericchapman profile image Eric Chapman ・3 min read

I develop in Javascript, Python, PHP and Ruby. By far Ruby is my favorite programming language.

Together let start a journey and revisit our Ruby foundations.

Each post will include some theory but also exercise and solution.

If you have any questions/comments or your are new and need help, you can comment below or send me a message.

Ruby blocks?

Before understanding Procs and Lambda we have to understand what is a code block in Ruby:

numbers = [1, 2, 3, 4, 5]

# Ruby full code block
numbers.each do |num| 
  puts num

# or

# Ruby one line code block
numbers.each { |num| puts num }
Enter fullscreen mode Exit fullscreen mode

The num is the argument that will be supply by the .each method. Noted that the argument name are define between two pipe characters |num|

Inside the block we can do anything including using the num argument.

Define a block using Lambda

A lambda is a way to define a block and its parameters with some special syntax. You can save this lambda into a variable for later use

display_welcome = lambda { puts 'Hello World' }
# or
display_welcome = -> { puts 'Hello World' }

#call the lambda
Enter fullscreen mode Exit fullscreen mode

Lambda can also take arguments

display_welcome = ->(msg) { puts "Hello #{msg}" }

#call the lambda with arguments'Mike')
Enter fullscreen mode Exit fullscreen mode

Ruby Procs

Procs are very similar to Lamdbas. Actually Lambda is a type of Proc. The syntax is a bit different:

display_welcome = { |msg| puts "Hello #{msg}" }

#call the lambda with arguments'Mike')
Enter fullscreen mode Exit fullscreen mode

Both Procs and Lambda will get the same result. Using one or the other can be confusing. Here a list of there differences:

  • Lambdas are defined with -> {} and procs with {}.
  • Procs 'return' keyword return from the current calling method, while lambdas return from the lambda itself.
  • Procs don’t care about the correct number of arguments, while lambdas will raise an exception.


Ruby procs & lambdas also have another special attribute. When you create a Ruby proc, it captures the current execution scope with it.

This concept, which is sometimes called closure, means that a proc will carry with it values like local variables and methods from the context where it was defined.

They don’t carry the actual values, but a reference to them, so if the variables change after the proc is created, the proc will always have the latest version.

def call_proc(my_proc)
  msg = 'Hello All'
msg = 'Hello Mike'
my_proc = { puts msg }
puts call_proc(my_proc) # Hello Mike
Enter fullscreen mode Exit fullscreen mode

Why 'Hello Mike' and not 'Hello All'? The Proc closure is using the context (scope) variables where it was define.

The mgs variable inside the call_proc method is not part of the same context (scope) as the declaration. So have no influence on the Proc.

At this point is 100% normal that you don't understand that concept of closure. I decide to write this info here because it is related to Procs and Lambda but chance are that it is too hard to gap for the moment... And that ok! I will not ask anything about it in the following exercise don't worry...


Create a little program that:

  • Create a variable that store a Lambda block
  • The lambda bloc will have 2 arguments.
  • Those arguments will be added (ex. a + b) inside the lambda block body.
  • Execute the lambda block
  • Display to console the result


addition = ->(a, b) { a + b }
puts, 20) # 30
Enter fullscreen mode Exit fullscreen mode


That's it for today. The journey just started, stay tune for the next post very soon. (later today or tomorrow)

If you have any comments or questions please do so here or send me a message on twitter.

I am new on twitter so if you want to make me happy
Follow me: Follow @justericchapman

Discussion (0)

Forem Open with the Forem app