Bird
0
0

What is the purpose of attr_writer in a Ruby class?

easy📝 Conceptual Q1 of 15
Ruby - Classes and Objects
What is the purpose of attr_writer in a Ruby class?
AIt deletes an instance variable.
BIt creates a setter method for an instance variable.
CIt creates both getter and setter methods for an instance variable.
DIt creates a getter method for an instance variable.
Step-by-Step Solution
Solution:
  1. Step 1: Understand attr_writer functionality

    attr_writer creates a setter method that allows you to assign a value to an instance variable from outside the class.
  2. Step 2: Differentiate from attr_reader and attr_accessor

    attr_reader creates only getter methods, attr_accessor creates both getter and setter methods.
  3. Final Answer:

    It creates a setter method for an instance variable. -> Option B
  4. Quick Check:

    attr_writer = setter method [OK]
Quick Trick: attr_writer lets you set values from outside the class [OK]
Common Mistakes:
  • Confusing attr_writer with attr_reader
  • Thinking attr_writer creates getter methods
  • Assuming attr_writer creates both getter and setter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes