Bird
0
0

Find the bug in this Ruby code: ```ruby def print_info(opts = {}) puts "Name: #{opts[:name]}" puts "Age: #{opts[:age]}" end print_info(name: 'Bob', age: 40, city: 'NYC') ```

medium📝 Debug Q7 of 15
Ruby - Hashes
Find the bug in this Ruby code: ```ruby def print_info(opts = {}) puts "Name: #{opts[:name]}" puts "Age: #{opts[:age]}" end print_info(name: 'Bob', age: 40, city: 'NYC') ```
ANo bug; extra keys in hash are ignored safely
BError because city key is unexpected
CSyntax error in method definition
DMethod should raise error for unknown keys
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash parameter flexibility

    Ruby methods accept hashes with extra keys without error.
  2. Step 2: Confirm output behavior

    Only :name and :age keys are used; :city is ignored safely.
  3. Final Answer:

    No bug; extra keys in hash are ignored safely -> Option A
  4. Quick Check:

    Extra hash keys do not cause errors [OK]
Quick Trick: Extra keys in hash do not cause errors [OK]
Common Mistakes:
  • Expecting error on unknown keys
  • Thinking method must list all keys explicitly
  • Confusing hash keys with method parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes