Challenge - 5 Problems
Keyword Arguments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method with keyword arguments
What is the output of this Ruby code?
Ruby
def greet(name:, greeting: "Hello") "#{greeting}, #{name}!" end puts greet(name: "Alice")
Attempts:
2 left
💡 Hint
Check the default value of the greeting keyword argument.
✗ Incorrect
The method greet requires a keyword argument 'name' and has a default for 'greeting'. Since only 'name' is passed, 'greeting' uses its default 'Hello'.
❓ Predict Output
intermediate2:00remaining
Keyword argument error
What error does this Ruby code produce?
Ruby
def add(x:, y:) x + y end add(1, 2)
Attempts:
2 left
💡 Hint
Check how the method expects arguments to be passed.
✗ Incorrect
The method expects keyword arguments, but positional arguments are given, causing ArgumentError.
🔧 Debug
advanced2:00remaining
Fix the keyword argument usage
This code raises an error. Which option fixes it so it prints "Sum is 7"?
Ruby
def sum_numbers(a:, b:) puts "Sum is #{a + b}" end sum_numbers(3, 4)
Attempts:
2 left
💡 Hint
Keyword arguments must be passed with their names.
✗ Incorrect
Only option A correctly passes both arguments as keywords matching the method signature.
❓ Predict Output
advanced2:00remaining
Keyword arguments with double splat operator
What is the output of this Ruby code?
Ruby
def details(**info) info.keys.sort.join(",") end puts details(age: 30, name: "Bob", city: "NY")
Attempts:
2 left
💡 Hint
The method collects all keyword arguments into a hash and sorts keys alphabetically.
✗ Incorrect
The keys are :age, :name, :city. Sorted alphabetically they are age, city, name.
🧠 Conceptual
expert2:00remaining
Behavior of keyword arguments with defaults and overrides
Given this method:
def configure(timeout: 5, retries: 3)
"Timeout: #{timeout}, Retries: #{retries}"
end
What is the output of configure(timeout: 10)?Attempts:
2 left
💡 Hint
Only the timeout argument is overridden; retries uses default.
✗ Incorrect
The method uses the default retries value 3 because it is not passed in the call.