📖 Scenario: You are building a simple greeting system where users can provide their name and optional details like age and city. You want to use a Ruby method that accepts these details as named parameters using a hash.
🎯 Goal: Create a Ruby method that uses a hash as named parameters to greet a user with their name and optionally include their age and city in the greeting.
📋 What You'll Learn
Create a method called greet_user that accepts a single hash parameter named details.
The details hash must have a required key :name.
Optionally, the details hash can have keys :age and :city.
Inside the method, build a greeting string that always includes the :name.
If :age is provided, include it in the greeting.
If :city is provided, include it in the greeting.
Call the method with different hashes to test the greetings.
💡 Why This Matters
🌍 Real World
Using hashes as named parameters is common in Ruby gems and libraries to allow flexible method calls with optional settings.
💼 Career
Understanding this pattern helps you read and write Ruby code that is clean, flexible, and easy to maintain, which is valuable in Ruby development jobs.
Progress0 / 4 steps
1
Create the greet_user method with a details hash parameter
Write a method called greet_user that takes one parameter named details. Inside the method, create a variable greeting and set it to the string "Hello, " plus the value of details[:name].
Ruby
Hint
Use details[:name] to access the name from the hash.
2
Add optional age and city to the greeting inside greet_user
Inside the greet_user method, add code to check if details[:age] exists. If it does, append ", age " plus the age to the greeting string. Then check if details[:city] exists. If it does, append ", from " plus the city to the greeting string.
Ruby
Hint
Use if details[:age] and if details[:city] to check for optional keys.
3
Return the greeting string from the method
Modify the greet_user method to return the greeting string at the end.
Ruby
Hint
Use return greeting to send the greeting string back to the caller.
4
Call greet_user with different hashes and print the results
Call greet_user with the hash {name: "Alice"} and print the result. Then call it with {name: "Bob", age: 30} and print the result. Finally, call it with {name: "Carol", city: "Paris"} and print the result.
Ruby
Hint
Use puts greet_user({name: "Alice"}) to print the greeting.
Practice
(1/5)
1. What is the main advantage of using a hash as named parameters in a Ruby method?
easy
A. It improves the method's execution speed significantly.
B. It forces the method to accept only one argument.
C. It makes the method call clearer by naming each argument explicitly.
D. It prevents any default values from being used.
Solution
Step 1: Understand named parameters purpose
Named parameters improve code readability by explicitly naming each argument passed to a method.
Step 2: Compare with other options
Options B, C, and D are incorrect because using a hash does not limit argument count to one, does not speed up execution notably, and does not prevent default values.
Final Answer:
It makes the method call clearer by naming each argument explicitly. -> Option C
Quick Check:
Named parameters = clearer calls [OK]
Hint: Named parameters improve clarity by naming each argument [OK]
Common Mistakes:
Thinking it limits arguments to one
Assuming it speeds up code
Believing it disables default values
2. Which of the following is the correct syntax to define a method using a hash for named parameters in Ruby?
easy
A. def greet(name: options = {})
B. def greet(name, options = {})
C. def greet(options: {})
D. def greet(options = [])
Solution
Step 1: Identify correct method parameter syntax
In Ruby, to accept named parameters as a hash, you define the method with a parameter defaulting to an empty hash, like options = {}.
Step 2: Check each option
def greet(name, options = {}) uses options = {} correctly. def greet(name: options = {}) uses invalid syntax. def greet(options: {}) tries to use keyword arguments incorrectly. def greet(options = []) uses an array instead of a hash.
Final Answer:
def greet(name, options = {}) -> Option B
Quick Check:
Hash parameter = options = {} [OK]
Hint: Use options = {} to accept named parameters as a hash [OK]
Common Mistakes:
Using square brackets [] instead of curly braces {}
Confusing keyword arguments with hash parameters
Incorrect syntax like name: options = {}
3. What will be the output of the following Ruby code?
A. Use options[:age] || 18 inside the string interpolation.
B. Set default value in method definition like def person_info(options = {age: 18}).
C. Raise an error if :age is missing to force passing it.
D. Use options.fetch(:age) without default to get 18.
Solution
Step 1: Understand default value requirement
We want to show 18 if :age is missing in options.
Step 2: Use Ruby's || operator for default
Using options[:age] || 18 returns options[:age] if present, otherwise 18.
Step 3: Check other options
Set default value in method definition like def person_info(options = {age: 18}). sets default hash but can be overridden incorrectly. Raise an error if :age is missing to force passing it. raises error, not default. Use options.fetch(:age) without default to get 18. fetch without default raises error if missing.
Final Answer:
Use options[:age] || 18 inside the string interpolation. -> Option A
Quick Check:
Default with || operator = 18 [OK]
Hint: Use options[:key] || default_value for defaults [OK]
Common Mistakes:
Setting default hash in parameter incorrectly
Using fetch without default causing error
Raising error instead of providing default
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.