Blocks, procs, lambdas

Methods that yield

A method runs the attached block wherever it calls yield, pausing itself until the block finishes. A method may yield several times or not at all. The predicate block_given? reports whether a caller attached a block, so yielding stays safe.

def twice yield yield end twice do puts "hi" end

ruby twice.rb

hi
hi

Block params with &

Prefix the last parameter with &block to capture the block as an ordinary object. Calling its call method runs the stored code, possibly much later. The same object can also travel onward into another method.

def run(&block) puts block.class block.call end run do puts "hello" end

ruby capture.rb

Proc
hello

Procs

The method proc builds a block object that can sit in a variable and run later through call. Procs are lenient about arguments: missing ones arrive as nil, and extra ones are quietly ignored.

hi = proc { |name| puts "hi #{name}" } puts hi.class hi.call("Ada")

ruby proc.rb

Proc
hi Ada

Lambdas vs procs

The method lambda builds a stricter cousin of a proc: a wrong argument count raises an ArgumentError at once. The predicate lambda? tells the two apart, returning true only for lambdas. Reach for lambdas when the stored code should behave like a strict function.

add = lambda { |a, b| puts a + b } add.call(2, 3) say = proc { |name| puts "hi #{name}" } puts add.lambda? puts say.lambda?

ruby lambda.rb

5
true
false

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which keyword runs the block a caller attached?

    Answer

    yield

    yield runs the block at that point and pauses the method until it finishes.

  2. What does this program print? def twice yield yield end twice do puts "hi" end

    Answer

    hi / hi

    The method yields twice, so the block runs twice.

  3. What does block_given? report?

    Answer

    Whether the current method was called with a block

    It makes a yield safe when the caller may skip the block.

  4. What is the class of the object a &block parameter captures?

    Answer

    Proc

    A block becomes a Proc, and lambda? tells a lambda apart from a plain proc.

  5. What does this program print? def run(&block) puts block.class block.call end run do puts "hello" end

    Answer

    Proc / hello

    The class is printed first, then call runs the stored block.

  6. How does a proc treat a missing argument?

    Answer

    It arrives as nil

    A proc is lenient about the count, unlike a lambda.

  7. How does a lambda treat a wrong argument count?

    Answer

    It raises ArgumentError at once

    A lambda behaves like a strict method, which is its point.

  8. What does proc { |a, b| [a, b] }.call(1) return?

    Answer

    [1, nil]

    The missing second argument arrives as nil in a proc.

  9. What does this program print? add = lambda { |a, b| puts a + b } add.call(2, 3) puts add.lambda? say = proc { |name| puts "hi #{name}" } puts say.lambda?

    Answer

    5 / true / false

    The lambda adds, and lambda? is true only for the lambda.

  10. What happens to extra arguments passed to a proc?

    Answer

    They are quietly ignored

    A proc is lenient in both directions, unlike a lambda.

Article author: Arthur Isaev