Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Metaprogramming Fundamentals
What will be the output of the following Ruby code?
require 'ostruct'
person = OpenStruct.new
person.age = 30
puts person.age
AError: undefined method 'age='
Bnil
C30
D0
Step-by-Step Solution
Solution:
  1. Step 1: Understand OpenStruct dynamic property assignment

    OpenStruct allows adding properties dynamically by assignment, so person.age = 30 sets the age property to 30.
  2. Step 2: Output the property value

    Using puts person.age prints the value 30 stored in the age property.
  3. Final Answer:

    30 -> Option C
  4. Quick Check:

    Dynamic property assignment = 30 [OK]
Quick Trick: Assign properties directly, then access them normally [OK]
Common Mistakes:
  • Expecting nil for uninitialized property
  • Thinking dynamic assignment causes error
  • Confusing with normal Struct behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes