0
0
Rubyprogramming~30 mins

Keyword arguments in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Keyword Arguments in Ruby Methods
📖 Scenario: You are creating a simple program to greet people with personalized messages. You want to use keyword arguments in Ruby methods to make your greetings clear and flexible.
🎯 Goal: Build a Ruby method that uses keyword arguments to greet a person with their name and age. Then call the method with different keyword arguments and print the greetings.
📋 What You'll Learn
Create a method called greet that takes keyword arguments name and age
Inside the method, create a greeting string using the name and age arguments
Call the greet method with keyword arguments for name and age
Print the greeting returned by the greet method
💡 Why This Matters
🌍 Real World
Keyword arguments help make your Ruby methods clear and flexible, especially when many options or settings are needed. This is common in web apps, scripts, and libraries.
💼 Career
Understanding keyword arguments is important for writing clean, maintainable Ruby code in jobs like backend development, automation scripting, and working with Ruby frameworks like Rails.
Progress0 / 4 steps
1
Create the greet method with keyword arguments
Write a method called greet that takes two keyword arguments: name: and age:. Inside the method, create a variable message that stores the string "Hello, my name is #{name} and I am #{age} years old." Use string interpolation exactly as shown.
Ruby
Need a hint?

Use def greet(name:, age:) to define the method with keyword arguments. Use double quotes and #{} for string interpolation.

2
Call the greet method with keyword arguments
Call the greet method with name: set to "Alice" and age: set to 30. Store the result in a variable called greeting.
Ruby
Need a hint?

Call the method with greet(name: "Alice", age: 30) and assign it to greeting.

3
Return the greeting message from the method
Modify the greet method to return the message variable so that the greeting string is returned when the method is called.
Ruby
Need a hint?

Add return message as the last line inside the method.

4
Print the greeting message
Write a puts statement to print the greeting variable to the console.
Ruby
Need a hint?

Use puts greeting to print the greeting string.