0
0
Rubyprogramming~10 mins

Keyword arguments in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Keyword arguments
Define method with keyword args
Call method with keywords
Match call keywords to params
Execute method body using keywords
Return result or output
The method is defined with named keyword arguments. When called, the keywords match parameters, then the method runs using those values.
Execution Sample
Ruby
def greet(name:, age:)
  "Hello, #{name}! You are #{age} years old."
end

puts greet(name: "Alice", age: 30)
Defines a method with keyword arguments and calls it with named values to print a greeting.
Execution Table
StepActionKeyword ArgumentValueResult/Output
1Define method greet with keywordsname, agenone yetMethod ready
2Call greet with keywordsname"Alice"Waiting for method body
3Call greet with keywordsage30Waiting for method body
4Match keywords to parametersname"Alice"name = "Alice"
4Match keywords to parametersage30age = 30
5Execute method bodyname, age"Alice", 30"Hello, Alice! You are 30 years old."
6puts outputs stringnonenoneHello, Alice! You are 30 years old.
💡 Method finishes after returning the greeting string and puts prints it.
Variable Tracker
VariableStartAfter CallAfter MatchFinal
nameundefinedpassed "Alice""Alice""Alice"
ageundefinedpassed 303030
Key Moments - 3 Insights
Why do we write name: and age: in the method definition?
Because these define keyword arguments that must be passed by name when calling the method, as shown in execution_table rows 1 and 2.
What happens if we call greet without one keyword argument?
Ruby will raise an error because the method expects all keyword arguments to be provided, as implied by the matching step in execution_table row 4.
Can we change the order of keyword arguments when calling the method?
Yes, because keywords match by name, not position, so order does not matter (see execution_table rows 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'age' after matching keywords to parameters?
Aundefined
B"Alice"
C30
Dnil
💡 Hint
Check execution_table row 4 where 'age' is matched to 30.
At which step does the method actually produce the greeting string?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the step where method body executes and returns the string (execution_table row 5).
If we call greet(age: 30, name: "Alice") instead, what changes in the execution_table?
AThe matching order of keywords changes but final values stay the same
BThe method will error because order changed
CThe output string changes
DThe method ignores the age argument
💡 Hint
Keyword arguments match by name, so order does not affect final variable values (see key_moments answer 3).
Concept Snapshot
def method_name(keyword1:, keyword2:)
  # use keyword1 and keyword2 inside
end

Call with method_name(keyword1: value1, keyword2: value2)

- Keywords must be named when calling
- Order does not matter
- Missing keywords cause errors
Full Transcript
This example shows how Ruby methods can use keyword arguments. The method greet is defined with two keywords: name and age. When calling greet, we pass values by naming them, like name: "Alice" and age: 30. Ruby matches these names to the parameters inside the method. Then the method uses these values to build a greeting string. Finally, puts prints the greeting. Keyword arguments help make code clearer by naming what each value means. They also allow arguments in any order but require all keywords to be given.