Bird
0
0

Identify the error in this code snippet using OpenStruct:

medium📝 Debug Q14 of 15
Ruby - Metaprogramming Fundamentals
Identify the error in this code snippet using OpenStruct:
require 'ostruct'
person = OpenStruct.new
person[:name] = "Bob"
puts person.name
Aperson.name is undefined because name was never set
BMissing parentheses in OpenStruct.new
CCannot use puts with OpenStruct properties
DOpenStruct does not support []= for setting properties
Step-by-Step Solution
Solution:
  1. Step 1: Check how properties are set in OpenStruct

    OpenStruct properties are set by dot notation (e.g., person.name = "Bob"), not by bracket notation.
  2. Step 2: Analyze the code behavior

    Using person[:name] = "Bob" tries to use []= method which OpenStruct does not support, causing an error.
  3. Final Answer:

    OpenStruct does not support []= for setting properties -> Option D
  4. Quick Check:

    Use dot notation, not []= [OK]
Quick Trick: Set properties with dot, not brackets [OK]
Common Mistakes:
  • Using []= to set OpenStruct properties
  • Assuming OpenStruct supports hash-like access
  • Ignoring error messages

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes