📖 Scenario: Imagine you are organizing a small event and you want to send personalized messages to each guest. You want to use a simple way to apply a custom action to each guest's name.
🎯 Goal: You will create a list of guest names, set up a block to customize messages, and then use the block to print personalized greetings for each guest. This will show how blocks let you pass custom code to methods in Ruby.
📋 What You'll Learn
Create an array called guests with the exact names: 'Alice', 'Bob', 'Charlie'
Create a variable called message_block that holds a block which takes one parameter name and returns the string "Hello, #{name}! Welcome to the event."
Use the map method on guests with the message_block to generate personalized greetings for each guest
Print each greeting exactly as returned by the block
💡 Why This Matters
🌍 Real World
Blocks let you write flexible code that can do different things with the same method, like sending custom messages to guests or processing lists of data.
💼 Career
Understanding blocks is essential for Ruby developers because many Ruby libraries and frameworks use blocks to let you customize behavior easily.
Progress0 / 4 steps
1
Create the guest list
Create an array called guests with these exact names: 'Alice', 'Bob', and 'Charlie'.
Ruby
Hint
Use square brackets [] to create an array and separate names with commas.
2
Create a block for personalized messages
Create a variable called message_block that holds a block. The block should take one parameter called name and return the string "Hello, #{name}! Welcome to the event." using string interpolation.
Ruby
Hint
Use Proc.new { |name| ... } to create a block and return the greeting string.
3
Use the block with each guest
Use the map method on the guests array with the message_block to get a greeting for each guest. Store the greetings in a new array called greetings.
Ruby
Hint
Use map with &message_block to apply the block to each guest and collect results.
4
Print the personalized greetings
Use a for loop to print each greeting from the greetings array exactly as it is.
Ruby
Hint
Use for greeting in greetings and puts greeting to print each message.
Practice
(1/5)
1. What is a block in Ruby?
easy
A. A type of variable that stores numbers
B. A chunk of code passed to a method to be executed
C. A method that returns a value
D. A special kind of loop that runs forever
Solution
Step 1: Understand what a block is
A block is a piece of code enclosed in braces {} or do..end that you can pass to a method.
Step 2: Identify the role of a block
Blocks allow methods to execute the passed code at specific times, making code flexible.
Final Answer:
A chunk of code passed to a method to be executed -> Option B
Quick Check:
Block = code passed to method [OK]
Hint: Blocks are code chunks given to methods to run [OK]
Common Mistakes:
Thinking blocks are variables
Confusing blocks with methods
Believing blocks are loops
2. Which of the following is the correct syntax to pass a block to a method in Ruby?
easy
A. method_name { |param| puts param }
B. method_name (|param| puts param)
C. method_name -> { puts param }
D. method_name [|param| puts param]
Solution
Step 1: Recognize Ruby block syntax
Blocks are passed using curly braces {} or do..end after the method call.
Step 2: Match the correct syntax
method_name { |param| puts param } uses curly braces with a block parameter and code inside, which is correct.
Final Answer:
method_name { |param| puts param } -> Option A
Quick Check:
Curly braces or do..end define blocks [OK]
Hint: Use {} or do..end after method for blocks [OK]
Step 1: Understand the method and block interaction
The method greet calls yield with the string 'Ruby', which passes it to the block parameter 'name'.
Step 2: Evaluate the block execution
The block prints "Hello, #{name}!" where name is 'Ruby', so it prints "Hello, Ruby!".
Final Answer:
Hello, Ruby! -> Option C
Quick Check:
yield passes 'Ruby' to block [OK]
Hint: yield runs block with given argument [OK]
Common Mistakes:
Thinking yield returns method name
Confusing block parameter with string literal
Expecting syntax error due to yield
4. Identify the error in this Ruby code:
def repeat
3.times do
yield
end
end
repeat { puts 'Hi' }
medium
A. SyntaxError due to missing block parameter
B. Method repeat is undefined
C. RuntimeError because yield has no block
D. No error, code runs and prints 'Hi' three times
Solution
Step 1: Analyze the method definition
The method repeat calls 3.times and yields to the block each time.
Step 2: Check the block passed to repeat
The block prints 'Hi', so it will print 'Hi' three times without error.
Final Answer:
No error, code runs and prints 'Hi' three times -> Option D
Quick Check:
yield with block runs fine [OK]
Hint: yield needs a block; here block is given [OK]
Common Mistakes:
Assuming yield needs parameters
Thinking missing block parameter causes error
Confusing method name with undefined
5. How can blocks make Ruby code more flexible? Choose the best explanation.
hard
A. Blocks allow methods to accept custom code to run, changing behavior without rewriting methods
B. Blocks store data permanently for later use
C. Blocks replace variables to save memory
D. Blocks automatically optimize code speed
Solution
Step 1: Understand the role of blocks in flexibility
Blocks let methods run different code passed by the user, so methods can do many tasks without changing their code.
Step 2: Evaluate the options
Blocks allow methods to accept custom code to run, changing behavior without rewriting methods. This is the best explanation. The other choices are wrong: blocks do not replace variables to save memory, store data permanently for later use, or automatically optimize code speed.
Final Answer:
Blocks allow methods to accept custom code to run, changing behavior without rewriting methods -> Option A
Quick Check:
Blocks = flexible method behavior [OK]
Hint: Blocks let methods run custom code passed in [OK]
Common Mistakes:
Thinking blocks store data like variables
Believing blocks speed up code automatically
Confusing blocks with memory management
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.