Bird
0
0

Identify the error in the following Ruby method using hash named parameters:

medium📝 Debug Q14 of 15
Ruby - Hashes
Identify the error in the following Ruby method using hash named parameters:
def display(options = {})
  puts "Title: #{options[:title]}"
  puts "Author: #{options[:author]}"
end

display(title: "Ruby Guide")
AIt prints Title but prints nothing for Author without error.
BIt raises an error because :author key is missing in options.
CSyntax error due to missing parentheses in method call.
DNo error; it prints Title and Author correctly.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method call and parameters

    The method display is called with a hash containing only :title. The :author key is missing.
  2. Step 2: Understand Ruby hash access behavior

    Accessing a missing key like options[:author] returns nil, which prints as empty string without error.
  3. Final Answer:

    It prints Title but prints nothing for Author without error. -> Option A
  4. Quick Check:

    Missing key returns nil, no error [OK]
Quick Trick: Missing hash keys return nil, print empty string [OK]
Common Mistakes:
MISTAKES
  • Expecting a runtime error for missing keys
  • Thinking missing keys print 'nil' string
  • Assuming syntax error without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes