Parameters with default values in Ruby - Time & Space Complexity
Let's see how using default values for parameters affects how long a program takes to run.
We want to know if adding default values changes the work the program does as input grows.
Analyze the time complexity of the following code snippet.
def greet(name = "Friend")
puts "Hello, #{name}!"
end
greet("Alice")
greet()
This code defines a method that says hello to a name, or to "Friend" if no name is given.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The method runs a simple print statement once per call.
- How many times: Each call runs the print once; no loops or recursion inside.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 print operations |
| 100 | 100 print operations |
| 1000 | 1000 print operations |
Pattern observation: The work grows directly with how many times the method is called, not affected by default values.
Time Complexity: O(n)
This means the time grows in a straight line with the number of calls, whether or not default values are used.
[X] Wrong: "Using default values makes the method slower because it has to check the defaults every time."
[OK] Correct: The check for default values is very quick and does not add extra loops or big work, so it does not change how time grows with input size.
Understanding how default parameters affect time helps you explain your code clearly and shows you know what really matters for performance.
"What if the method had a loop inside that ran based on the parameter value? How would the time complexity change?"
Practice
def greet(name = "Guest")Solution
Step 1: Understand default parameter purpose
Default parameters provide a fallback value if no argument is given.Step 2: Apply to the example method
Indef greet(name = "Guest"), if no name is passed, "Guest" is used.Final Answer:
It allows the method to be called without that argument, using the default instead. -> Option DQuick Check:
Default parameter = fallback value [OK]
- Thinking default parameters are mandatory
- Assuming missing arguments cause errors
- Confusing default values with method overloading
Solution
Step 1: Review Ruby default parameter syntax
Default values are set with= valueafter the parameter name.Step 2: Check each option
def add(a, b = 5); a + b; end usesb = 5correctly. def add(a =, b 5); a + b; end has invalid syntax. def add(a, b: 5); a + b; end uses keyword argument style, not default parameter. def add(a, b); b = 5; a + b; end assigns inside method body, not default parameter.Final Answer:
def add(a, b = 5); a + b; end -> Option AQuick Check:
Default param syntax = param = value [OK]
- Using colon instead of equals for default values
- Placing = before parameter name
- Assigning default inside method body instead of parameter list
def multiply(x, y = 2) x * y end puts multiply(4) puts multiply(4, 3)
Solution
Step 1: Analyze method calls with default parameter
First call:multiply(4)uses default y=2, so 4*2=8. Second call:multiply(4,3)uses y=3, so 4*3=12.Step 2: Determine output of puts statements
Outputs are printed on separate lines: 8 then 12.Final Answer:
8 12 -> Option AQuick Check:
Default param used when missing = 8, explicit param = 12 [OK]
- Expecting error when argument is missing
- Confusing default parameter with keyword arguments
- Mixing up output values
def greet(name = "Guest", age)
"Hello #{name}, you are #{age} years old."
endSolution
Step 1: Check parameter order rules in Ruby
Parameters with default values must be placed after parameters without defaults.Step 2: Analyze given method signature
Here,namehas default, butagedoes not and comes after. This causes syntax error.Final Answer:
Default parameters must come after non-default parameters. -> Option CQuick Check:
Default params after required params [OK]
- Placing default parameters before required ones
- Assuming return statement is mandatory
- Misreading string interpolation syntax
order that takes a product name and an optional quantity with default 1. Which is the best way to define it to allow calling order("apple") and order("apple", 5)?Solution
Step 1: Identify correct parameter order and defaults
Required parameters come first, then optional with default values.Step 2: Evaluate each option
def order(product, quantity = 1); "Order #{quantity} #{product}(s)"; end correctly placesproductfirst andquantitywith default 1 second. def order(product = "apple", quantity); "Order #{quantity} #{product}(s)"; end has default before required, invalid. def order(quantity = 1, product); "Order #{quantity} #{product}(s)"; end swaps order incorrectly. def order(product, quantity); quantity ||= 1; "Order #{quantity} #{product}(s)"; end assigns default inside method but requires both arguments.Final Answer:
def order(product, quantity = 1); "Order #{quantity} #{product}(s)"; end -> Option BQuick Check:
Required first, default second = def order(product, quantity = 1); "Order #{quantity} #{product}(s)"; end [OK]
- Placing default parameter before required
- Assigning default inside method body instead of parameter list
- Confusing parameter order
Ready to prove it?
Pass the test and this pattern moves unknown
