Skip to content
Poignant Guide Ruby Q&A

How do attr_accessor, attr_reader and attr_writer work in Ruby?

Staff answerWritten by Odile Marchetti · Classes, modules and objects0 replies

Short answer

They write small methods for you. attr_reader creates a method that returns an instance variable, attr_writer creates a method that sets it, and attr_accessor creates both. Ruby hides instance variables from the outside by default, so these three are how other code is allowed to read or change an object's state.

Written by Odile Marchetti · Staff: Ruby basics, objects and the Poignant Guide ·
CitePoignant Guide Ruby Q&A, "How do attr_accessor, attr_reader and attr_writer work in Ruby?", https://poignantguide.net/answers/read/how-do-attr-accessor-attr-reader-and-attr-writer-work-in-ruby/, accessed 2026-11-19.
  1. Staff: Ruby basics, objects and the Poignant Guide ·

    The short version, runnable

    An instance variable is a variable that starts with @ and belongs to one object. Paste this into irb:

    class Dog
      attr_accessor :name   # read and write
      attr_reader   :id     # read only
      attr_writer   :mood   # write only
    
      def initialize(name)
        @name = name
        @id = 7
      end
    end
    
    d = Dog.new("Rex")
    p d.name
    # => "Rex"
    d.name = "Max"
    p d.name
    # => "Max"
    p d.id
    # => 7
    d.id = 9
    # NoMethodError: undefined method `id=' for an instance of Dog
    

    Without the attr_ lines you could not even write d.name. Ruby objects keep their instance variables private, and the only way in is a method. That is the whole reason these helpers exist.

    What Ruby writes for you

    The Module documentation says attr_accessor "creates an instance variable (@name) and a corresponding access method to read it" and "also creates a method called name= to set the attribute". So these two classes behave the same:

    class Short
      attr_accessor :name
    end
    
    class Long
      def name
        @name
      end
    
      def name=(value)
        @name = value
      end
    end
    

    A method ending in = is a setter, and Ruby lets you call it with spaces around the equals sign, so d.name = "Max" really means d.name=("Max"). You can see what was generated:

    p Dog.instance_methods(false).sort
    # => [:id, :mood=, :name, :name=]
    

    Each helper returns an array of the method names it defined, and takes symbols (:name) or strings. If symbols are new to you, this thread on symbols explains why the colon is there.

    Which one do I pick?

    You write Reading obj.x Writing obj.x = 1 Use it when
    attr_reader :x yes no the value is set once, in initialize
    attr_writer :x no yes other code gives it a value but never asks for it back
    attr_accessor :x yes yes callers may read and change it

    Start with attr_reader. Promote to attr_accessor only when something outside the class needs to change the value. A smaller door is easier to keep tidy. On this site's copy of Why's (Poignant) Guide to Ruby, the classes chapter shows attr_accessor on a Fox class and notes that attr_reader and attr_writer give you half each.

    Mistakes beginners make

    Forgetting self when assigning inside the class. This is the classic one. Inside a method, lives = 5 creates a brand new local variable, it does not call the setter:

    class Cat
      attr_accessor :lives
    
      def initialize
        @lives = 9
      end
    
      def lose_life
        lives = @lives - 1   # local variable, the cat is unchanged
      end
    
      def lose_life_properly
        self.lives = lives - 1   # calls lives=
      end
    end
    
    c = Cat.new
    c.lose_life
    p c.lives
    # => 9
    c.lose_life_properly
    p c.lives
    # => 8
    

    Writing @lives -= 1 also works, and skips the setter altogether. Reading with lives needs no self; only assignment does.

    Expecting attr_writer to give you a reader. Call d.mood on the Dog above and Ruby raises a NoMethodError saying there is no method named mood (it may even suggest mood= as the near miss). If you need both, use attr_accessor.

    Using attr_accessor for everything. It lets any code overwrite the value with anything, including nil. When a value needs checking, write the setter yourself and keep the reader from the helper:

    class Temp
      attr_reader :degrees
    
      def initialize(degrees)
        @degrees = degrees
      end
    
      def degrees=(value)
        raise ArgumentError, "too cold" if value < -273
        @degrees = value
      end
    end
    

    Making a private attribute public by accident. Helpers called after private become private methods. Private methods can only be called without a receiver, apart from self.name = ..., as the syntax documentation on classes and modules describes. That is a useful trick when only the class itself should touch a value.

    Check this in order

    1. Is the error undefined method ending in =? You are missing a writer. Add attr_writer or attr_accessor.
    2. Is it undefined method without the =? You are missing a reader.
    3. Does an assignment inside a method seem to do nothing? Add self. in front of the name.
    4. Does the attribute come back nil? Check that initialize actually assigns @name, and check the spelling of the instance variable.
    5. Still stuck? Print d.instance_variables to see what the object really holds. In the Dog above it gives [:@name, :@id, :@mood] once the mood has been set.

    Checked on 12 November 2026.

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

Poignant Guide Ruby Q&A. Questions and answers on learning Ruby and programming: syntax, blocks, objects, gems, Rails, debugging and reading the Poignant Guide.