Bird
0
0

How can you modify this method to accept any extra keyword arguments without error?

hard📝 Application Q9 of 15
Ruby - Methods
How can you modify this method to accept any extra keyword arguments without error?
def log_event(event:, user:)
  puts "Event: #{event}, User: #{user}"
end
ARemove all parameters
BAdd **kwargs parameter: def log_event(event:, user:, **kwargs)
CAdd *args parameter: def log_event(event:, user:, *args)
DUse positional arguments instead
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to accept extra keyword arguments

    Ruby uses double splat (**kwargs) to capture extra keyword arguments.
  2. Step 2: Check options

    Add **kwargs parameter: def log_event(event:, user:, **kwargs) correctly adds **kwargs. Add *args parameter: def log_event(event:, user:, *args) uses *args which captures positional args, not keywords. Options A, B, and D remove or change parameters incorrectly.
  3. Final Answer:

    Add **kwargs parameter: def log_event(event:, user:, **kwargs) -> Option B
  4. Quick Check:

    Use **kwargs to accept extra keyword arguments [OK]
Quick Trick: Use **kwargs to capture extra keyword args [OK]
Common Mistakes:
  • Using *args instead of **kwargs for keywords
  • Removing parameters instead of adding **kwargs
  • Switching to positional arguments incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes