Bird
0
0

Which of the following is the correct way to access an attribute named age from an OpenStruct object called person?

easy📝 Syntax Q3 of 15
Ruby - Metaprogramming Fundamentals
Which of the following is the correct way to access an attribute named age from an OpenStruct object called person?
Aperson.age
Bperson.get(:age)
Cperson[:age]
Dperson->age
Step-by-Step Solution
Solution:
  1. Step 1: Recall attribute access in OpenStruct

    OpenStruct attributes are accessed like object properties using dot notation, e.g., person.age.
  2. Step 2: Evaluate options

    person.age uses dot notation correctly. person[:age] uses hash syntax which is invalid. person.get(:age) uses a non-existent method. person->age uses invalid Ruby syntax.
  3. Final Answer:

    person.age -> Option A
  4. Quick Check:

    Access OpenStruct attribute = dot notation [OK]
Quick Trick: Use dot notation to access OpenStruct attributes [OK]
Common Mistakes:
  • Using hash syntax
  • Calling non-existent methods
  • Using invalid Ruby operators

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes