I am having a hard time understanding attr_accessor
in Ruby.
Can someone explain this to me?

- 7,904
- 4
- 42
- 42

- 25,132
- 33
- 116
- 189
-
24Please see also [Why use Ruby's attr_accessor, attr_reader and attr_writer?](http://stackoverflow.com/questions/5046831/why-use-rubys-attr-accessor-attr-reader-and-attr-writer). – Andrew Marshall Mar 20 '12 at 04:36
-
1Does attr_accessor work this same way in Git? I'm finding that some tutorials don't explain enough and others assume prior knowledge. – Angelfirenze Dec 01 '14 at 23:33
-
11@Angelfirenze, `git` has nothing to do with `attr_accessor`. Git is a version control software, whereas `attr_accessor` is a [method in Ruby](http://ruby-doc.org/core-2.3.0/Module.html#method-i-attr_accessor). – Uzbekjon Apr 14 '16 at 14:52
20 Answers
Let's say you have a class Person
.
class Person
end
person = Person.new
person.name # => no method error
Obviously we never defined method name
. Let's do that.
class Person
def name
@name # simply returning an instance variable @name
end
end
person = Person.new
person.name # => nil
person.name = "Dennis" # => no method error
Aha, we can read the name, but that doesn't mean we can assign the name. Those are two different methods. The former is called reader and latter is called writer. We didn't create the writer yet so let's do that.
class Person
def name
@name
end
def name=(str)
@name = str
end
end
person = Person.new
person.name = 'Dennis'
person.name # => "Dennis"
Awesome. Now we can write and read instance variable @name
using reader and writer methods. Except, this is done so frequently, why waste time writing these methods every time? We can do it easier.
class Person
attr_reader :name
attr_writer :name
end
Even this can get repetitive. When you want both reader and writer just use accessor!
class Person
attr_accessor :name
end
person = Person.new
person.name = "Dennis"
person.name # => "Dennis"
Works the same way! And guess what: the instance variable @name
in our person object will be set just like when we did it manually, so you can use it in other methods.
class Person
attr_accessor :name
def greeting
"Hello #{@name}"
end
end
person = Person.new
person.name = "Dennis"
person.greeting # => "Hello Dennis"
That's it. In order to understand how attr_reader
, attr_writer
, and attr_accessor
methods actually generate methods for you, read other answers, books, ruby docs.

- 544
- 10
- 26

- 37,015
- 6
- 38
- 43
-
53@hakunin - thank you for that clear answer. What is missing for me is why the Ruby syntax suggests a colon ':' for the instance variables in the attr_* statement? It seems that it would be more straight forward to use the same '@' syntax that is used elsewhere in the class to refer to the variable. – Will May 24 '12 at 18:34
-
223@WilliamSmith To answer your question you need to understand that `attr_accessor` is a method called on the current class, and `:name` is a parameter you pass to that method. It's not a special syntax, it's a simple method call. If you were to give it `@name` variable, it wouldn't make sense, because @name would contain `nil`. So it would be like writing `attr_accessor nil`. You are not passing it a variable that it needs to create, you are passing the name that you want the variable to be called. – Max Chernyak May 24 '12 at 19:30
-
26@hakunin - That makes total sense. I've just today been learning that ruby is actually 'running' as it parses through a file and that every statement and expression is actually a method call on some object. Including attr_accessor. Very helpful. – Will May 25 '12 at 00:35
-
1I see now. So that's the equivalent of automatic properties in C#. Awesome! – Richard77 Nov 18 '12 at 01:56
-
what if we use attr_reader , I guess still we can write to that attribute inside inilialize , for example attr_reader :name def initialize() @name = 'DDD' end – Buminda Sep 21 '13 at 09:08
-
5@Buminda yes, but method `name` and variable `@name` are not the same thing. Don't confuse them. You have instance variable `@name` in your class, and you define `attr_reader :name` to be able to read it from the outside. Without `attr_reader` there is no simple way you can access `@name` outside of your class. – Max Chernyak Sep 21 '13 at 16:35
-
Why not just give the object an instance variable and get and set it directly instead of using getter and setter methods? I don't see how this saves any time or work over accessing the variables directly. – tadasajon Feb 18 '15 at 15:17
-
1@JonCrowell there is (almost) no such thing as "getting/setting variables directly" from outside of the object. You can only do that inside the object. You can't do `person.@name` <- this doesn't exist. Ruby has special methods: `person.instance_variable_get('@name')` or `person.instance_variable_set('@name')` but you probably don't want to typically use them. – Max Chernyak Feb 18 '15 at 19:29
-
i still dont get why there is colon, what does colon do here. i get it's being passed as a parameter but is it a symbol – Muhammad Umer Aug 10 '15 at 21:16
-
@hakunin Very neat explanation in simple language. I rarely find one these days.. thanks – whitehat Feb 18 '16 at 23:30
-
1@MuhammadUmer I know this is dated but I figured I'd leave this for a future person wondering the same thing. its a colon because its a Symbol and this is is what [Object#send](http://ruby-doc.org/core-2.3.0/Object.html#method-i-send) expects to receive. While the call can take a String, it will have to convert said String to a Symbol. So by passing a Symbol you save a step. – engineerDave Apr 18 '16 at 22:31
-
One note missing from all comments so far is that since `attr_reader`, `attr_writer` and `attr_accessor` are all implemented in C, they are actually faster at run-time than writing the equivalent getters and setters manually in ruby. – MissingHandle Jan 11 '17 at 19:19
-
@MaxChernyakakahakunin - why is it neccessary to use the `@` infront of `name` in your example above. The same without the `@` works too in this case. but i notice some inconsistencies if i drop off the `@` from the instance variable with an `attr_accessor` being set. thanks – OK999 Dec 28 '17 at 04:30
-
What if var as 'attr_reader' only, in this case when is the value needed to be set initially, since we can't do var = "", which throws exceptions it's readonly, conclusion, if i need var for read only means where to set it for the first time doesn't it give exception if we do 'var=' method, what do need to do in that case ? and say I would like to make var 'x' as attr_accessor and later lock it to readonly mode after first assign(where it is done) for no further changes Thank you all. – codemilan Feb 16 '18 at 11:03
-
After reading a bunch of confusing explanations, this one made me *actually* understand wtf these methods mean. Thank you! – Erica May 14 '18 at 03:59
-
@OK999 When you are using `@name` you are referring to your object's internal instance variable directly. When you are using `name`, you are calling a method by that name. This method can do any number of things, and it doesn't have to specifically return the variable `@name`. – Max Chernyak May 15 '18 at 02:08
-
1@codemilan Your classes can define constructors (e.g. `def initialize`) which take parameters, and assign those parameters to internal instance variables. E.g. `person = Person.new('Codemilan')` and then `person.name # => "Codemilan"`. In this scenario you aren't actually using the name writer method. – Max Chernyak May 15 '18 at 02:10
-
Given my name is Dennis and this answer is the first time i ever understood this topic I feel that somehow you actually wrote this one for me! – Dennis Sep 21 '18 at 00:00
-
Very well explained. I'm new to Ruby, and, I must say that it seems to me that the language shamelessly mangles established semantics. This is a case in point: "accessor" and "mutator" methods are methods that read and write attributes, respectively. At least they have been until now: now an accessor both accesses *and* mutates an attribute! Why not simply use `attr_readwriter` or some such, I wonder, instead of this malapropism. Not whimsical enough, I suppose. :) – BobRodes Jan 10 '19 at 19:10
-
@BobRodes Correct me if I'm wrong, but when you say "until now", you are implicitly referring to another language, where this word was used to indicate reading. There is nothing in the word "access" that inherently makes it about reading. Could it be that the other language chose a poor word? Shouldn't we be fixing historical mistakes as we advance? However, keep in mind that ruby was started in 1995, and is currently 24 yo. It has just as much right to establish terminology as any other language. – Max Chernyak Jan 10 '19 at 23:09
-
Reading any definition of the word "access" suggests that it is indeed synonymous with the idea of reading, e.g. "to open or load (a computer file, an internet site, etc.)." To access something is NOT to change it. Changing is mutating. Therefore, in most languages, accessor methods are methods which access (read) an attribute, and mutator methods are methods which change (write) an attribute. This terminology is common to most languages, including C/C++, C#, Java and several others. However, I find that Smalltalk uses the term "accessor methods" to mean getters and setters. (...) – BobRodes Jan 10 '19 at 23:54
-
So, perhaps Ruby, which, as I understand it, has its roots in Smalltalk, eschews the fixing of Smalltalk's "historical mistake" that pretty much every other OO language since then has fixed, due to Smalltalk's use of the term being semantically inconsistent with the English language. Anyway, that's the other side of your argument. The larger concern that I have is the lack of semantic consistency in Ruby; while I understand that the numerous ways to do the same thing are intended to make the language friendly to people already familiar with a wide variety of languages, (...) – BobRodes Jan 11 '19 at 00:04
-
I have to say that the result appears to be that it is possible thereby for Ruby programmers to write code that is unintelligible to other Ruby programmers who use different ways to accomplish the same things. The fact that there is a "style guide" to address this issue suggests that this unintelligibility is a genuine problem, and must be addressed through agreeing to conventions, rather than adopting semantics that are inherent in the language itself. I therefore have trouble seeing the point of having several different ways to do the same thing: (...) – BobRodes Jan 11 '19 at 00:05
-
If that means that nobody can understand anyone else's code unless they all agree to stick to the same ways of doing things, what's the point of having different ways to do them in the first place? – BobRodes Jan 11 '19 at 00:08
-
@BobRodes It's hard to make an objective argument about the term "accessor" being wrong. You can access something in order to read it or change it. As far as consistency, if you understand ruby fundamentals, you can understand any kind of ruby code. The consistency just comes at a lower level, enabling you to implement what looks like language features in the language itself, all thanks to executable class bodies. – Max Chernyak Feb 26 '19 at 15:43
-
@MaxChernyak I picked up Ruby on Jan. 7, so when I wrote this my dive into Ruby was four days old. Since then, I have developed a much better understanding of Ruby fundamentals, and see exactly (and also agree with) what you are saying. Ruby is internally very consistently organized, and if you understand the underlying architecture, the "syntax sugar" is actually impressive with respect to how quickly you can put something complex together. As for "accessor" methods, it's hard to make an objective argument about any matter of semantics, since the idea of relating a word to a meaning is (...) – BobRodes Feb 27 '19 at 22:55
-
inherently a subjective one. However, one can make arguments about common usage. It is true that you have to access something in order to change it, but it is also true that access does not in any way imply change. So what I'm seeing now is that Ruby doesn't really make a distinction about whether a method is a mutating method at the level of the method signature. Methods either mutate their caller or they don't, but there isn't anything inherent in the method signature or the way arguments are passed that defines that. (Ending a name with `!` is a convention rather than a language structure.) – BobRodes Feb 27 '19 at 23:01
-
hey @MaxChernyak , when we have a class with many functions(methods) inside and it's very hard to call all the functions one by one using the object created for the respective class. Is there any efficient way available to call and excecute all the functions inside the class with a single command? thank you! – ajay_speed Oct 27 '22 at 06:02
-
1@ajay_speed Your use case is a bit unclear. Feel free to ask a new question on SO, and link it to me here. I'll go take a look. – Max Chernyak Oct 27 '22 at 15:24
attr_accessor is just a method. (The link should provide more insight with how it works - look at the pairs of methods generated, and a tutorial should show you how to use it.)
The trick is that class
is not a definition in Ruby (it is "just a definition" in languages like C++ and Java), but it is an expression that evaluates. It is during this evaluation when the attr_accessor
method is invoked which in turn modifies the current class - remember the implicit receiver: self.attr_accessor
, where self
is the "open" class object at this point.
The need for attr_accessor
and friends, is, well:
Ruby, like Smalltalk, does not allow instance variables to be accessed outside of methods1 for that object. That is, instance variables cannot be accessed in the
x.y
form as is common in say, Java or even Python. In Rubyy
is always taken as a message to send (or "method to call"). Thus theattr_*
methods create wrappers which proxy the instance@variable
access through dynamically created methods.Boilerplate sucks
Hope this clarifies some of the little details. Happy coding.
1 This isn't strictly true and there are some "techniques" around this, but there is no syntax support for "public instance variable" access.

- 1
- 1
-
When you say attr_accessor is "just a method" i get it. But what is the syntax used to *call* that method called? I'm having trouble finding the section in the ruby documentation that talks about syntax like some_method :name => "whatever", :notherName, :etc – B T Feb 05 '15 at 01:19
-
It appears to be called automatically whenever you access an instance variable using '@' or '.' notation. – Jim Grisham Feb 17 '22 at 05:47
attr_accessor
is (as @pst stated) just a method. What it does is create more methods for you.
So this code here:
class Foo
attr_accessor :bar
end
is equivalent to this code:
class Foo
def bar
@bar
end
def bar=( new_value )
@bar = new_value
end
end
You can write this sort of method yourself in Ruby:
class Module
def var( method_name )
inst_variable_name = "@#{method_name}".to_sym
define_method method_name do
instance_variable_get inst_variable_name
end
define_method "#{method_name}=" do |new_value|
instance_variable_set inst_variable_name, new_value
end
end
end
class Foo
var :bar
end
f = Foo.new
p f.bar #=> nil
f.bar = 42
p f.bar #=> 42

- 296,393
- 112
- 651
- 745
-
8This is a great example of where metaprogramming is used in even the most beginner-level scenarios. Very nice. – John Simon Feb 26 '14 at 07:33
-
2I was looking for an implementation sketch of `attr_accessor` and found here at last ! Though it solved my problem, but I am curious to know where(book/official doc) can I find an implementation example like this? – Wasif Hossain May 11 '16 at 18:31
attr_accessor
is very simple:
attr_accessor :foo
is a shortcut for:
def foo=(val)
@foo = val
end
def foo
@foo
end
it is nothing more than a getter/setter for an object

- 5,106
- 1
- 24
- 19
-
11your answer is fine. 'Shortcut' means "a shorter alternative route" according to my dictionary, not "syntax sugar" or "macro interpreted by the interpreter". – bowsersenior Dec 06 '10 at 21:57
Basically they fake publicly accessible data attributes, which Ruby doesn't have.

- 12,879
- 1
- 32
- 39
-
5Though this comment isn't entirely useful, it is true. Highlights the fact that public data attributes don't exist outside of "get" methods in Ruby, which is really useful info for someone trying to learn the language. – Eric Dand Oct 07 '13 at 07:54
-
4This really shouldn't be downvoted. As a non-Ruby guy trying to figure this stuff out, this answer is very helpful! – Brad Dec 19 '13 at 19:26
-
1
It is just a method that defines getter and setter methods for instance variables. An example implementation would be:
def self.attr_accessor(*names)
names.each do |name|
define_method(name) {instance_variable_get("@#{name}")} # This is the getter
define_method("#{name}=") {|arg| instance_variable_set("@#{name}", arg)} # This is the setter
end
end

- 234,037
- 30
- 302
- 389
-
1
-
1This was a really helpful code snippet to solve another question that I had related to metaprogramming. – alexventuraio Mar 30 '20 at 22:16
If you are familiar with OOP concept, You must familiar with getter and setter method. attr_accessor does the same in Ruby.
Getter and Setter in General Way
class Person
def name
@name
end
def name=(str)
@name = str
end
end
person = Person.new
person.name = 'Eshaan'
person.name # => "Eshaan"
Setter Method
def name=(val)
@name = val
end
Getter method
def name
@name
end
Getter and Setter method in Ruby
class Person
attr_accessor :name
end
person = Person.new
person.name = "Eshaan"
person.name # => "Eshaan"

- 14,192
- 3
- 29
- 60

- 341
- 4
- 7
-
2perfect explanation! It is a very handy behavior and can be overridden too very easily. – Rubyrider Feb 08 '15 at 07:35
I faced this problem as well and wrote a somewhat lengthy answer to this question. There are some great answers on this already, but anyone looking for more clarification, I hope my answer can help
Initialize Method
Initialize allows you to set data to an instance of an object upon creation of the instance rather than having to set them on a separate line in your code each time you create a new instance of the class.
class Person
def initialize(name)
@name = name
end
def greeting
"Hello #{@name}"
end
end
person = Person.new("Denis")
puts person.greeting
In the code above we are setting the name “Denis” using the initialize method by passing Dennis through the parameter in Initialize. If we wanted to set the name without the initialize method we could do so like this:
class Person
attr_accessor :name
# def initialize(name)
# @name = name
# end
def greeting
"Hello #{name}"
end
end
person = Person.new
person.name = "Dennis"
puts person.greeting
In the code above, we set the name by calling on the attr_accessor setter method using person.name, rather than setting the values upon initialization of the object.
Both “methods” of doing this work, but initialize saves us time and lines of code.
This is the only job of initialize. You cannot call on initialize as a method. To actually get the values of an instance object you need to use getters and setters (attr_reader (get), attr_writer(set), and attr_accessor(both)). See below for more detail on those.
Getters, Setters (attr_reader, attr_writer, attr_accessor)
Getters, attr_reader: The entire purpose of a getter is to return the value of a particular instance variable. Visit the sample code below for a breakdown on this.
class Item
def initialize(item_name, quantity)
@item_name = item_name
@quantity = quantity
end
def item_name
@item_name
end
def quantity
@quantity
end
end
example = Item.new("TV",2)
puts example.item_name
puts example.quantity
In the code above you are calling the methods “item_name” and “quantity” on the instance of Item “example”. The “puts example.item_name” and “example.quantity” will return (or “get”) the value for the parameters that were passed into the “example” and display them to the screen.
Luckily in Ruby there is an inherent method that allows us to write this code more succinctly; the attr_reader method. See the code below;
class Item
attr_reader :item_name, :quantity
def initialize(item_name, quantity)
@item_name = item_name
@quantity = quantity
end
end
item = Item.new("TV",2)
puts item.item_name
puts item.quantity
This syntax works exactly the same way, only it saves us six lines of code. Imagine if you had 5 more state attributable to the Item class? The code would get long quickly.
Setters, attr_writer: What crossed me up at first with setter methods is that in my eyes it seemed to perform an identical function to the initialize method. Below I explain the difference based on my understanding;
As stated before, the initialize method allows you to set the values for an instance of an object upon object creation.
But what if you wanted to set the values later, after the instance was created, or change them after they have been initialized? This would be a scenario where you would use a setter method. THAT IS THE DIFFERENCE. You don’t have to “set” a particular state when you are using the attr_writer method initially.
The code below is an example of using a setter method to declare the value item_name for this instance of the Item class. Notice that we continue to use the getter method attr_reader so that we can get the values and print them to the screen, just in case you want to test the code on your own.
class Item
attr_reader :item_name
def item_name=(str)
@item_name = (str)
end
end
The code below is an example of using attr_writer to once again shorten our code and save us time.
class Item
attr_reader :item_name
attr_writer :item_name
end
item = Item.new
puts item.item_name = "TV"
The code below is a reiteration of the initialize example above of where we are using initialize to set the objects value of item_name upon creation.
class Item
attr_reader :item_name
def initialize(item_name)
@item_name = item_name
end
end
item = Item.new("TV")
puts item.item_name
attr_accessor: Performs the functions of both attr_reader and attr_writer, saving you one more line of code.

- 4,532
- 2
- 36
- 61

- 1,284
- 17
- 38
Simple Explanation Without Any Code
Most of the above answers use code. This explanation attempts to answer it without using any, via an analogy/story:
Outside parties cannot access internal CIA secrets
Let's imagine a really secret place: the CIA. Nobody knows what's happening in the CIA apart from the people inside the CIA. In other words, external people cannot access any information in the CIA. But because it's no good having an organisation that is completely secret, certain information is made available to the outside world - only things that the CIA wants everyone to know about of course: e.g. the Director of the CIA, how environmentally friendly this department is compared to all other government departments etc. Other information: e.g. who are its covert operatives in Iraq or Afghanistan - these types of things will probably remain a secret for the next 150 years.
If you're outside the CIA you can only access the information that it has made available to the public. Or to use CIA parlance you can only access information that is "cleared".
The information that the CIA wants to make available to the general public outside the CIA are called: attributes.
The meaning of read and write attributes:
In the case of the CIA, most attributes are "read only". This means if you are a party external to the CIA, you can ask: "who is the director of the CIA?" and you will get a straight answer. But what you cannot do with "read only" attributes is to make changes changes in the CIA. e.g. you cannot make a phone call and suddenly decide that you want Kim Kardashian to be the Director, or that you want Paris Hilton to be the Commander in Chief.
If the attributes gave you "write" access, then you could make changes if you want to, even if you were outside. Otherwise, the only thing you can do is read.
In other words accessors allow you to make inquiries, or to make changes, to organisations that otherwise do not let external people in, depending on whether the accessors are read or write accessors.
Objects inside a class can easily access each other
- On the other hand, if you were already inside the CIA, then you could easily call up your CIA operative in Kabul because this information is easily accessible given you are already inside. But if you're outside the CIA, you simply will not be given access: you will not be able to know who they are (read access), and you will not be able to change their mission (write access).
Exact same thing with classes and your ability to access variables, properties and methods within them. HTH! Any questions, please ask and I hope i can clarify.

- 33,477
- 14
- 111
- 80
-
Your explanation makes sense! +1 Sorry, are you sure that the expression "information which is cleared by the CIA is right? – kouty Dec 11 '16 at 12:06
-
there are various "clearance" levels in the CIA: e.g. Top Secret (nobody but the Prez),or public trust (everyone can read that info). The CIA actually provides a lot of very cool facts! – BenKoshy Dec 11 '16 at 22:19
-
You deserve the upvote just for the Kardashian, Paris Hilton examples :) I thought it was bad enough with Trump for President, imagine those two in charge omg! – rmcsharry Mar 24 '19 at 12:03
-
I think part of what confuses new Rubyists/programmers (like myself) is:
"Why can't I just tell the instance it has any given attribute (e.g., name) and give that attribute a value all in one swoop?"
A little more generalized, but this is how it clicked for me:
Given:
class Person
end
We haven't defined Person as something that can have a name or any other attributes for that matter.
So if we then:
baby = Person.new
...and try to give them a name...
baby.name = "Ruth"
We get an error because, in Rubyland, a Person class of object is not something that is associated with or capable of having a "name" ... yet!
BUT we can use any of the given methods (see previous answers) as a way to say, "An instance of a Person class (baby
) can now have an attribute called 'name', therefore we not only have a syntactical way of getting and setting that name, but it makes sense for us to do so."
Again, hitting this question from a slightly different and more general angle, but I hope this helps the next instance of class Person who finds their way to this thread.

- 197
- 3
- 11
Simply put it will define a setter and getter for the class.
Note that
attr_reader :v is equivalant to
def v
@v
end
attr_writer :v is equivalant to
def v=(value)
@v=value
end
So
attr_accessor :v which means
attr_reader :v; attr_writer :v
are equivalant to define a setter and getter for the class.

- 5,955
- 7
- 48
- 50
Simply attr-accessor
creates the getter
and setter
methods for the specified attributes

- 348
- 4
- 13
Another way to understand it is to figure out what error code it eliminates by having attr_accessor
.
Example:
class BankAccount
def initialize( account_owner )
@owner = account_owner
@balance = 0
end
def deposit( amount )
@balance = @balance + amount
end
def withdraw( amount )
@balance = @balance - amount
end
end
The following methods are available:
$ bankie = BankAccout.new("Iggy")
$ bankie
$ bankie.deposit(100)
$ bankie.withdraw(5)
The following methods throws error:
$ bankie.owner #undefined method `owner'...
$ bankie.balance #undefined method `balance'...
owner
and balance
are not, technically, a method, but an attribute. BankAccount class does not have def owner
and def balance
. If it does, then you can use the two commands below. But those two methods aren't there. However, you can access attributes as if you'd access a method via attr_accessor
!! Hence the word attr_accessor
. Attribute. Accessor. It accesses attributes like you would access a method.
Adding attr_accessor :balance, :owner
allows you to read and write balance
and owner
"method". Now you can use the last 2 methods.
$ bankie.balance
$ bankie.owner

- 5,129
- 12
- 53
- 87
Despite the large number of existing answers, none of them seems to me to explain the actual mechanism involved here. It's metaprogramming; it takes advantage of the following two facts:
You can modify a module / class on the fly
A module / class declaration is itself executable code
Okay, so imagine the following:
class Nameable
def self.named(whatvalue)
define_method :name do whatvalue end
end
end
We are declaring a class method named
which, when called with a value, creates an instance method called name
which returns that value. That is the metaprogramming part.
Now we'll subclass that class:
class Dog < Nameable
named "Fido"
end
What on earth did we just do? Well, in the class declaration, executable code executes with reference to the class. So the bare word named
is actually a call to the class method named
, which we inherited from Nameable; and we are passing the string "Fido"
as the argument.
And what does the class method named
do? It creates an instance method called name
, which returns that value. So now, behind the scenes, Dog has a method that looks like this:
def name
"Fido"
end
Don't believe me? Then watch this little move:
puts Dog.new.name #=> Fido
Why did I tell you all that? Because what I just did with named
for Nameable is almost exactly what attr_accessor
does for Module. When you say attr_accessor
you are calling a class method (inherited from Module) that creates instance methods. In particular, it creates a getter and setter method for the instance property whose name you provide as argument, so that you don't have to write those getter and setter methods yourself.

- 515,959
- 87
- 875
- 1,141
Defines a named attribute for this module, where the name is symbol.id2name, creating an instance variable (@name) and a corresponding access method to read it. Also creates a method called name= to set the attribute.
module Mod
attr_accessor(:one, :two)
end
Mod.instance_methods.sort #=> [:one, :one=, :two, :two=]

- 1,324
- 14
- 20
To summarize an attribute accessor aka attr_accessor gives you two free methods.
Like in Java they get called getters and setters.
Many answers have shown good examples so I'm just going to be brief.
#the_attribute
and
#the_attribute=
In the old ruby docs a hash tag # means a method. It could also include a class name prefix... MyClass#my_method

- 2,203
- 21
- 20
I am new to ruby and had to just deal with understanding the following weirdness. Might help out someone else in the future. In the end it is as was mentioned above, where 2 functions (def myvar, def myvar=) both get implicitly for accessing @myvar, but these methods can be overridden by local declarations.
class Foo
attr_accessor 'myvar'
def initialize
@myvar = "A"
myvar = "B"
puts @myvar # A
puts myvar # B - myvar declared above overrides myvar method
end
def test
puts @myvar # A
puts myvar # A - coming from myvar accessor
myvar = "C" # local myvar overrides accessor
puts @myvar # A
puts myvar # C
send "myvar=", "E" # not running "myvar =", but instead calls setter for @myvar
puts @myvar # E
puts myvar # C
end
end

- 1,726
- 2
- 16
- 23
Attributes and accessor methods
Attributes are class components that can be accessed from outside the object. They are known as properties in many other programming languages. Their values are accessible by using the "dot notation", as in object_name.attribute_name. Unlike Python and a few other languages, Ruby does not allow instance variables to be accessed directly from outside the object.
class Car
def initialize
@wheels = 4 # This is an instance variable
end
end
c = Car.new
c.wheels # Output: NoMethodError: undefined method `wheels' for #<Car:0x00000000d43500>
In the above example, c is an instance (object) of the Car class. We tried unsuccessfully to read the value of the wheels instance variable from outside the object. What happened is that Ruby attempted to call a method named wheels within the c object, but no such method was defined. In short, object_name.attribute_name tries to call a method named attribute_name within the object. To access the value of the wheels variable from the outside, we need to implement an instance method by that name, which will return the value of that variable when called. That's called an accessor method. In the general programming context, the usual way to access an instance variable from outside the object is to implement accessor methods, also known as getter and setter methods. A getter allows the value of a variable defined within a class to be read from the outside and a setter allows it to be written from the outside.
In the following example, we have added getter and setter methods to the Car class to access the wheels variable from outside the object. This is not the "Ruby way" of defining getters and setters; it serves only to illustrate what getter and setter methods do.
class Car
def wheels # getter method
@wheels
end
def wheels=(val) # setter method
@wheels = val
end
end
f = Car.new
f.wheels = 4 # The setter method was invoked
f.wheels # The getter method was invoked
# Output: => 4
The above example works and similar code is commonly used to create getter and setter methods in other languages. However, Ruby provides a simpler way to do this: three built-in methods called attr_reader, attr_writer and attr_acessor. The attr_reader method makes an instance variable readable from the outside, attr_writer makes it writeable, and attr_acessor makes it readable and writeable.
The above example can be rewritten like this.
class Car
attr_accessor :wheels
end
f = Car.new
f.wheels = 4
f.wheels # Output: => 4
In the above example, the wheels attribute will be readable and writable from outside the object. If instead of attr_accessor, we used attr_reader, it would be read-only. If we used attr_writer, it would be write-only. Those three methods are not getters and setters in themselves but, when called, they create getter and setter methods for us. They are methods that dynamically (programmatically) generate other methods; that's called metaprogramming.
The first (longer) example, which does not employ Ruby's built-in methods, should only be used when additional code is required in the getter and setter methods. For instance, a setter method may need to validate data or do some calculation before assigning a value to an instance variable.
It is possible to access (read and write) instance variables from outside the object, by using the instance_variable_get and instance_variable_set built-in methods. However, this is rarely justifiable and usually a bad idea, as bypassing encapsulation tends to wreak all sorts of havoc.

- 3,239
- 26
- 39
Hmmm. Lots of good answers. Here is my few cents on it.
attr_accessor
is a simple method that helps us in cleaning(DRY-ing) up the repeatinggetter and setter
methods.So that we can focus more on writing business logic and not worry about the setters and getters.

- 226,338
- 43
- 373
- 367

- 2,785
- 22
- 27
The main functionality of attr_accessor over the other ones is the capability of accessing data from other files.
So you usually would have attr_reader or attr_writer but the good news is that Ruby lets you combine these two together with attr_accessor. I think of it as my to go method because it is more well rounded or versatile.
Also, peep in mind that in Rails, this is eliminated because it does it for you in the back end. So in other words: you are better off using attr_acessor over the other two because you don't have to worry about being to specific, the accessor covers it all. I know this is more of a general explanation but it helped me as a beginner.
Hope this helped!

- 1
- 3