0
0
Rubyprogramming~10 mins

Attr_reader, attr_writer, attr_accessor in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Attr_reader, attr_writer, attr_accessor
Define class with attr_*
Create object instance
Access or modify attribute
attr_reader
Read value
attr_accessor
Read and write value
This flow shows how Ruby's attr_reader, attr_writer, and attr_accessor create methods to read, write, or both read and write object attributes.
Execution Sample
Ruby
class Person
  attr_reader :name
  attr_writer :age
  attr_accessor :city
end

p = Person.new
p.city = "NY"
Defines a Person class with different attribute accessors and sets the city attribute.
Execution Table
StepActionAttributeMethod CreatedOperationResult/Value
1Define attr_reader :namenamedef name; @name; endReadMethod to get @name created
2Define attr_writer :ageagedef age=(val); @age = val; endWriteMethod to set @age created
3Define attr_accessor :citycitydef city; @city; end and def city=(val); @city = val; endRead & WriteMethods to get and set @city created
4Create Person instance---Object p created
5Set p.city = "NY"citycity= method calledWrite@city set to "NY"
6Try to read p.namenamename method calledReadReturns nil (not set)
7Try to set p.age = 30ageage= method calledWrite@age set to 30
8Try to read p.ageageNo method createdReadError: undefined method 'age'
9Try to read p.citycitycity method calledReadReturns "NY"
💡 Execution stops after demonstrating read/write methods created and used for attributes.
Variable Tracker
VariableStartAfter Step 5After Step 7After Step 9
@namenilnilnilnil
@agenilnil3030
@citynil"NY""NY""NY"
Key Moments - 3 Insights
Why can we read p.name but not p.age?
Because attr_reader creates a method to read @name (see step 1 and 6), but attr_writer only creates a method to write @age (step 2 and 7). There is no method to read @age, so reading it causes an error (step 8).
What does attr_accessor do differently?
attr_accessor creates both read and write methods for the attribute (step 3). So for :city, we can both read and write its value (steps 5 and 9).
Why does p.city return "NY" after setting it?
Because attr_accessor created a getter method that returns the current value of @city, which was set to "NY" in step 5 and read in step 9.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens when we try to read p.age at step 8?
AIt returns the value 30
BIt causes an error because no read method exists
CIt returns nil
DIt sets @age to 30
💡 Hint
Check step 8 in the execution_table where reading p.age fails due to missing method.
At which step is the write method for :city created?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at step 3 where attr_accessor creates both read and write methods for city.
If we add attr_reader :age, what would change in the execution table?
AStep 5 would fail to set city
BStep 7 would cause an error
CStep 8 would return 30 instead of error
DNo change at all
💡 Hint
Adding attr_reader :age creates a read method, so reading p.age at step 8 would succeed.
Concept Snapshot
attr_reader :symbol  # creates getter method to read attribute
attr_writer :symbol  # creates setter method to write attribute
attr_accessor :symbol # creates both getter and setter
Use to avoid writing simple methods manually.
attr_reader allows reading only, attr_writer writing only, attr_accessor both.
Full Transcript
This visual execution shows how Ruby's attr_reader, attr_writer, and attr_accessor work. First, attr_reader creates a method to read an attribute's value. attr_writer creates a method to write or change the attribute's value. attr_accessor creates both read and write methods. We define a Person class with these attributes. When we create an instance and try to read or write attributes, only the methods created by attr_* work. For example, reading p.name works because of attr_reader, but reading p.age fails because only attr_writer was defined. Setting and reading p.city works because attr_accessor was used. This helps avoid writing simple getter and setter methods manually.