📖 Scenario: You are creating a simple Ruby program that uses yield to run blocks of code passed to a method. This is like giving your method a little helper to do extra work when you call it.
🎯 Goal: Build a Ruby method that uses yield to execute a block, and see how the block runs inside the method.
📋 What You'll Learn
Create a method called greet that uses yield
Pass a block to greet that prints a greeting message
Call the greet method with the block
Print the output of the block execution
💡 Why This Matters
🌍 Real World
Using <code>yield</code> lets Ruby methods run extra code blocks, which is useful for customizing behavior like logging, repeating tasks, or handling events.
💼 Career
Understanding <code>yield</code> is important for Ruby developers because many Ruby libraries and frameworks use blocks to make code flexible and clean.
Progress0 / 4 steps
1
Create the greet method
Write a method called greet that prints "Hello!" inside it.
Ruby
Hint
Use def greet to start the method and puts "Hello!" to print inside it.
2
Add yield inside greet
Add yield inside the greet method after the puts "Hello!" line.
Ruby
Hint
Just write yield on a new line inside the method after the greeting.
3
Call greet with a block
Call the greet method and pass a block that prints "Nice to meet you!".
Ruby
Hint
Call greet with curly braces and inside them write puts "Nice to meet you!".
4
Run the program and see the output
Run the program and print the output. The program should print Hello! and then Nice to meet you! on separate lines.
Ruby
Hint
Just run the program. It should print two lines: Hello! and Nice to meet you!.
Practice
(1/5)
1. What does the yield keyword do in a Ruby method?
easy
A. It runs the block of code passed to the method.
B. It returns the method's value immediately.
C. It defines a new method inside the current method.
D. It pauses the method execution permanently.
Solution
Step 1: Understand the role of yield
The yield keyword is used inside a method to call the block that was passed to that method.
Step 2: Differentiate from other keywords
Unlike return or method definitions, yield specifically executes the block given to the method.
Final Answer:
It runs the block of code passed to the method. -> Option A
Quick Check:
Yield executes block = A [OK]
Hint: Yield always runs the block given to the method [OK]
Common Mistakes:
Thinking yield returns method value
Confusing yield with return
Assuming yield defines a method
2. Which of the following is the correct syntax to use yield with an argument inside a method?
easy
A. def example; yield(5); end
B. def example; yield5; end
C. def example; yield = 5; end
D. def example; yield: 5; end
Solution
Step 1: Recall yield syntax with arguments
To pass arguments to the block, yield must be called with parentheses around the arguments, like yield(5).
Step 2: Check other options for syntax errors
Options B, C, and D are incorrect because they either miss parentheses or use invalid syntax.
Final Answer:
def example; yield(5); end -> Option A
Quick Check:
Yield with args uses parentheses = A [OK]
Hint: Use parentheses to pass arguments with yield [OK]
Common Mistakes:
Omitting parentheses when passing arguments
Using assignment (=) with yield
Using colon (:) instead of parentheses
3. What will be the output of the following Ruby code?