Bird
0
0

Why does this Ruby code raise an error?

medium📝 Debug Q7 of 15
Ruby - Methods
Why does this Ruby code raise an error?
def book(title:, author:)
  puts "#{title} by #{author}"
end

book(title: 'Ruby 101')
Atitle keyword argument is invalid
BMissing required keyword argument author
CMethod call uses positional arguments incorrectly
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check method parameters

    Both title and author are required keyword arguments without defaults.
  2. Step 2: Analyze method call

    Call provides only title, missing author, causing error.
  3. Final Answer:

    Missing required keyword argument author -> Option B
  4. Quick Check:

    All required keyword args must be provided [OK]
Quick Trick: All required keyword args must be passed [OK]
Common Mistakes:
MISTAKES
  • Assuming missing keyword args default to nil
  • Passing only some required keywords
  • Confusing positional with keyword arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes