Yield and Passing Blocks
Explore the yield keyword and how to pass blocks to methods dynamically.
Yield and Passing Blocks is a free Ruby Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.

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!' }3
Passing Parameters with Yield
The yield keyword can pass parameters to the block.
def square(num)
yield num * num
end
square(4) { |result| puts "Square: #{result}" }4
Checking if a Block is Given
You can check if a method received a block using block_given?.
def greet
if block_given?
yield
else
puts 'No block given'
end
end
greet
greet { puts 'Hello from block!' }5
Passing Blocks with &block
Blocks can be passed explicitly as a parameter using &block.
def execute_block(&block)
block.call if block
end
execute_block { puts 'Block executed!' }6
Using Yield Multiple Times
A method can call yield multiple times.
def repeat(times)
times.times { yield }
end
repeat(3) { puts 'Hello!' }7
Passing Blocks as Procs
Blocks can be converted to Procs and passed as arguments.
def execute_proc(proc)
proc.call
end
my_proc = Proc.new { puts 'Proc executed!' }
execute_proc(my_proc)8
9
Using Lambda as a Block
A lambda can be used in place of a block.
def execute_lambda(lmbd)
lmbd.call
end
my_lambda = -> { puts 'Lambda executed!' }
execute_lambda(my_lambda)10
Congratulations!
You have learned how to use yield and pass blocks in Ruby.
Next, you will learn about working with collections.

Frequently asked questions
Is the “Yield and Passing Blocks” lesson free?
Yes — the full text of “Yield and Passing Blocks” is free to read here on the web, and the Ruby Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.
What will I learn in “Yield and Passing Blocks”?
Explore the yield keyword and how to pass blocks to methods dynamically. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Ruby Academy?
No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Yield and Passing Blocks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Ruby Academy lesson?
Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Defining Methods
- Blocks, Procs, and Lambdas
- Yield and Passing Blocks