Bird
0
0

Why does Ruby use the * symbol before a parameter name to accept variable-length arguments?

hard📝 Conceptual Q10 of 15
Ruby - Methods
Why does Ruby use the * symbol before a parameter name to accept variable-length arguments?
ATo indicate the parameter is optional
BTo collect all extra arguments into a single array parameter
CTo mark the parameter as a block
DTo unpack an array into separate arguments
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of * in method parameters

    The * before a parameter name collects all remaining arguments into one array.
  2. Step 2: Differentiate from other uses of *

    To indicate the parameter is optional is incorrect because * does not mark optional parameters. To mark the parameter as a block is for blocks (&). To unpack an array into separate arguments is the opposite use of * (splat operator in calls).
  3. Final Answer:

    To collect all extra arguments into a single array parameter -> Option B
  4. Quick Check:

    *args collects extra arguments into array [OK]
Quick Trick: In method defs, * collects extra args into one array [OK]
Common Mistakes:
MISTAKES
  • Confusing *args with block parameters (&block)
  • Thinking * means optional parameter
  • Mixing * usage in calls vs definitions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes