Bird
0
0

Which of the following best describes attr_accessor in Ruby?

easy📝 Conceptual Q2 of 15
Ruby - Classes and Objects
Which of the following best describes attr_accessor in Ruby?
ACreates both getter and setter methods for an instance variable.
BCreates only a setter method for an instance variable.
CCreates a method to delete an instance variable.
DCreates only a getter method for an instance variable.
Step-by-Step Solution
Solution:
  1. Step 1: Recall attr_accessor purpose

    attr_accessor creates both getter and setter methods, allowing reading and writing of instance variables.
  2. Step 2: Compare with attr_reader and attr_writer

    attr_reader creates only getter, attr_writer only setter, attr_accessor does both.
  3. Final Answer:

    Creates both getter and setter methods for an instance variable. -> Option A
  4. Quick Check:

    attr_accessor = getter + setter [OK]
Quick Trick: attr_accessor gives read and write access [OK]
Common Mistakes:
  • Thinking attr_accessor creates only one method
  • Confusing attr_accessor with attr_reader or attr_writer
  • Assuming attr_accessor deletes variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes