Block,Lambda and Proc

3 Min. Read
Dec 8, 2019

Reference: https://www.rubyguides.com/2016/02/ruby-procs-and-lambdas/

Block

Ruby blocks are little anonymous functions that can be passed into methods. Blocks are enclosed in a do / end statement or between brackets {}, and they can have multiple arguments. The argument names are defined between two pipe | characters.

Blocks are mostly used with each.

For Example:

1
[1, 2, 3].each { |num| puts num }

|num| is block argument.

puts num is block body.

Yield Keyword

Yield is a Ruby keyword that calls a block when you use it.

It’s how methods USE blocks!

When you use the yield keyword, the code inside the block will run & do its work just like when you call a regular Ruby method.

1
2
3
4
5
6
def print_once
  yield
end
print_once { puts "Block is being run" }

# => Block is being run

This runs any block passed to print_once, as a result, “Block is being run” will be printed on the screen.

That yield can be used multiple times?

Every time you call yield, the block will run, so this is like calling the same method again.

1
2
3
4
5
6
7
8
def one_two_three
  yield 1
  yield 2
  yield 3
end

one_two_three { |number| puts number * 10 }
# 10, 20, 30

These arguments then become the block’s arguments.

In this example number.

Implicit vs Explicit Blocks

Blocks can be “explicit” or “implicit”.

Explicit means that you give it a name in your parameter list.

Note: You can pass an explicit block to another method or save it into a variable to use later.

1
2
3
4
5
def explicit_block(&block)
  block.call # same as yield
end

explicit_block { puts "Explicit block called" }

Notice the &block parameter.

That’s how you define the block’s name!

You can check if a block has been passed in with the block_given? method.

For Example:

1
2
3
4
5
6
7
8
9
10
11
def explicit_block(&block)
  if block_given?
    block.call # same as yield
  else
    puts "No block given"
  end
end

explicit_block #=> No block given
explicit_block { puts "Explicit block called" } #=> Explicit block called

Lambda

A lambda is a way to define a block & its parameters with some special syntax.

You can save this lambda into a variable for later use.

The syntax for defining a Ruby lambda looks like this:

1
newLambda = -> { puts "This is a lambda"}

You can also use the alternative syntax: lambda instead of ->.

Defining a lambda won’t run the code inside it, just like defining a method won’t run the method, you need to use the call method for that.

For Example:

1
2
3
4
5
newLambda = -> { puts "This is a lambda" }
newLambda.call


# "This is a lambda"

Lambdas can also take arguments.

For Example:

1
2
3
times_two = ->(x) { x * 2 }
times_two.call(10)
# 20

If you pass the wrong number of arguments to a lambda, it will raise an exception, just like a regular method.

Lambdas vs Procs

Procs are a very similar concept.

One of the differences is how you create them.

1
  my_proc = Proc.new { |x| puts x }

NOTE: There is no dedicated Lambda class. *A lambda is just a special Proc object. * If you take a look at the instance methods from Proc, you will notice there is a lambda? method.

Lambdas are defined with -> {} and procs with Proc.new {}.