0Pricing
Ruby Academy · Lesson

Yield and Passing Blocks

Explore the yield keyword and how to pass blocks to methods dynamically.

1

Yield and Passing Blocks

Ruby allows passing blocks to methods using the yield keyword.

In this lesson, you will learn how to use yield to execute a block inside a method.

Yield and Passing Blocks — illustration 1

2

Using Yield

The yield keyword runs the block given to the method.

def greet
  puts 'Before yield'
  yield
  puts 'After yield'
end

greet { puts 'Hello from the block!' }

All lessons in this course

  1. Defining Methods
  2. Blocks, Procs, and Lambdas
  3. Yield and Passing Blocks
← Back to Ruby Academy