📖 Scenario: You are creating a simple calculator that can add any number of numbers given to it. This is useful when you don't know in advance how many numbers the user wants to add.
🎯 Goal: Build a Ruby method that uses variable-length arguments (*args) to add any number of numbers and print the total sum.
📋 What You'll Learn
Create a method called add_numbers that accepts variable-length arguments using *args.
Inside the method, calculate the sum of all numbers passed in *args.
Call the method with a list of numbers to test it.
Print the result of the addition.
💡 Why This Matters
🌍 Real World
Variable-length arguments are useful in calculators, logging functions, or any place where the number of inputs can change.
💼 Career
Understanding how to handle flexible inputs is important for writing reusable and adaptable code in software development.
Progress0 / 4 steps
1
Create the add_numbers method with *args
Write a method called add_numbers that accepts variable-length arguments using *args. Do not add any code inside the method yet.
Ruby
Hint
Use def add_numbers(*args) to start the method.
2
Add code to sum all numbers in *args
Inside the add_numbers method, create a variable called total and set it to the sum of all numbers in args using the sum method.
Ruby
Hint
Use total = args.sum to add all numbers.
3
Make the method return the total sum
Add a line inside the add_numbers method to return the variable total.
Ruby
Hint
Use return total to send back the sum.
4
Call the method and print the result
Call the add_numbers method with the numbers 4, 7, 1, 8 and print the result using puts.
Ruby
Hint
Use puts add_numbers(4, 7, 1, 8) to show the sum.
Practice
(1/5)
1. What does *args do in a Ruby method?
easy
A. Makes the method return an array
B. Restricts the method to accept only one argument
C. Converts all arguments to strings automatically
D. Allows the method to accept any number of arguments as an array
Solution
Step 1: Understand the role of *args
*args collects all extra arguments passed to a method into an array.
Step 2: Recognize the flexibility it provides
This lets the method accept any number of arguments, not just one fixed number.
Final Answer:
Allows the method to accept any number of arguments as an array -> Option D
Quick Check:
*args = variable number of arguments [OK]
Hint: Remember *args packs all extra inputs into an array [OK]
Common Mistakes:
Thinking *args limits arguments to one
Confusing *args with return values
Assuming *args converts types automatically
2. Which of the following is the correct syntax to define a method that accepts variable-length arguments in Ruby?
easy
A. def example(args*)
B. def example(args)
C. def example(*args)
D. def example(&args)
Solution
Step 1: Identify the correct syntax for variable-length arguments
In Ruby, *args is used inside method parentheses to accept any number of arguments.
Step 2: Check each option
def example(*args) is the correct syntax. def example(args*) places the asterisk incorrectly. def example(args) accepts only one argument. def example(&args) uses & which is for blocks.
Final Answer:
def example(*args) -> Option C
Quick Check:
Method with *args = correct syntax [OK]
Hint: Look for asterisk before argument name inside parentheses [OK]
Common Mistakes:
Placing * after the argument name
Using & instead of * for arguments
Omitting the asterisk entirely
3. What will be the output of the following Ruby code?
def greet(*names)
names.join(", ")
end
puts greet("Alice", "Bob", "Carol")
medium
A. Alice, Bob, Carol
B. ["Alice", "Bob", "Carol"]
C. Alice Bob Carol
D. Error: wrong number of arguments
Solution
Step 1: Understand how *names works
The method collects all arguments into an array called names. Here, it will be ["Alice", "Bob", "Carol"].
Step 2: Analyze the join method
names.join(", ") joins array elements into a string separated by ", ". So the output is "Alice, Bob, Carol".
Final Answer:
Alice, Bob, Carol -> Option A
Quick Check:
Array joined with commas = Alice, Bob, Carol [OK]
Hint: Remember join adds separator between array elements [OK]
4. Identify the error in this Ruby method definition:
def sum_numbers(*nums)
total = 0
nums.each do |n|
total += n
end
total
end
puts sum_numbers(1, 2, '3')
medium
A. Syntax error in method definition
B. Cannot add string '3' to integer total
C. Missing return statement
D. Method does not accept variable arguments
Solution
Step 1: Check method logic with variable arguments
The method sums all numbers in nums using total += n.
Step 2: Identify type mismatch error
Passing '3' as a string causes an error when adding to integer total. Ruby cannot add string and integer directly.
Final Answer:
Cannot add string '3' to integer total -> Option B
Quick Check:
Adding string to integer causes error [OK]
Hint: Check argument types when summing inside *args method [OK]
Common Mistakes:
Ignoring type mismatch between string and integer
Assuming method needs explicit return
Thinking *args causes syntax error
5. You want to write a Ruby method combine that accepts any number of arrays and returns a single array with all elements combined. Which method definition correctly uses *args to achieve this?
hard
A. def combine(*arrays)
arrays.flatten
end
B. def combine(arrays)
arrays.flatten
end
C. def combine(*arrays)
arrays.join
end
D. def combine(*arrays)
arrays.map(&:size)
end
Solution
Step 1: Understand the goal
The method should accept multiple arrays and combine all their elements into one flat array.
Step 2: Analyze each option
def combine(*arrays)
arrays.flatten
end uses *arrays to accept variable arrays and calls flatten to merge them into one array. def combine(arrays)
arrays.flatten
end accepts only one argument (an array of arrays). def combine(*arrays)
arrays.join
end joins arrays into a string, not desired. def combine(*arrays)
arrays.map(&:size)
end returns sizes, not combined elements.
Final Answer:
def combine(*arrays)
arrays.flatten
end -> Option A
Quick Check:
Use *args and flatten to combine arrays [OK]
Hint: Use *args plus flatten to merge multiple arrays [OK]
Common Mistakes:
Not using *args for multiple arrays
Using join instead of flatten
Returning sizes instead of combined elements
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.