0
0
Rubyprogramming~15 mins

Block parameters in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Block Parameters in Ruby
📖 Scenario: You are organizing a small party and have a list of guests with their ages. You want to greet each guest personally and mention if they are an adult or not.
🎯 Goal: Learn how to use block parameters in Ruby to process each item in a list and print customized messages.
📋 What You'll Learn
Create an array of guest names and ages
Use a block with parameters to access each guest's name and age
Print a greeting message for each guest mentioning if they are an adult (age 18 or older) or not
💡 Why This Matters
🌍 Real World
Using block parameters helps process lists of data like guest lists, orders, or records easily and clearly.
💼 Career
Understanding block parameters is essential for Ruby developers to write clean, readable, and efficient code when working with collections.
Progress0 / 4 steps
1
Create the guests array
Create an array called guests with these exact elements: [['Alice', 17], ['Bob', 22], ['Charlie', 15]]
Ruby
Need a hint?

Use square brackets to create an array of arrays. Each inner array has a name and an age.

2
Set the adult age limit
Create a variable called adult_age and set it to 18
Ruby
Need a hint?

Just assign the number 18 to the variable adult_age.

3
Use block parameters to greet guests
Use guests.each do |name, age| to loop over the guests array and inside the block, create a variable status that is 'an adult' if age >= adult_age or 'not an adult' otherwise
Ruby
Need a hint?

Use the block parameters |name, age| to get each guest's details. Use a ternary operator to set status.

4
Print greeting messages
Inside the block, add a puts statement to print: "Hello, #{name}! You are #{status}."
Ruby
Need a hint?

Use puts with string interpolation to print the message.