# Attr\_writer vs attr\_accessor

**URL:** <https://www.ruby-forum.com/t/attr-writer-vs-attr-accessor/130194>\
**Category:** Ruby\
**Created:** [February 26, 2008, 9:30am UTC](https://www.ruby-forum.com/t/attr-writer-vs-attr-accessor/130194 "2008-02-26T09:30:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [February 26, 2008, 9:30am UTC](https://www.ruby-forum.com/t/attr-writer-vs-attr-accessor/130194/1 "2008-02-26T09:30:50Z")

</div>

Hello all,

From readings I understood that attr\_accessor is the one which has the  
property of attr\_reader and attr\_writer. If we define a variable as  
attr\_writer, we can use it to read and write as well. My question is;  
why do we need to use attr\_accessor since attr\_writer does the job of  
read and write?

-Sunil

---

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [February 26, 2008, 10:39am UTC](https://www.ruby-forum.com/t/attr-writer-vs-attr-accessor/130194/2 "2008-02-26T10:39:49Z")

</div>

On Tue, Feb 26, 2008 at 9:29 AM, ycsunil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all,
> 
> From readings I understood that attr\_accessor is the one which has the  
> property of attr\_reader and attr\_writer. If we define a variable as  
> attr\_writer, we can use it to read and write as well. My question is;  
> why do we need to use attr\_accessor since attr\_writer does the job of  
> read and write?

Did you try it? I get an error, when I use the following

irb(main):001:0\> class Foo  
irb(main):002:1\> attr\_writer :bar  
irb(main):003:1\> def initialize  
irb(main):004:2\> @bar = “hallo”  
irb(main):005:2\> end  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> Foo.new.bar  
NoMethodError: undefined method `bar’ for #\<Foo:0x2ad5640 @bar=“hallo”\>  
from (irb):7  
irb(main):008:0\>

attr\_writer just creates the bar=(value) method, no bar() method to  
access the attribute.

---

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [February 26, 2008, 10:44am UTC](https://www.ruby-forum.com/t/attr-writer-vs-attr-accessor/130194/3 "2008-02-26T10:44:45Z")

</div>

Sunil C. wrote:

> Hello all,
> 
> If we define a variable as  
> attr\_writer, we can use it to read and write as well.

Prove it.

---

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [February 26, 2008, 11:10am UTC](https://www.ruby-forum.com/t/attr-writer-vs-attr-accessor/130194/4 "2008-02-26T11:10:39Z")

</div>

On Feb 26, 2:38 pm, Thomas W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):008:0\>
> 
> attr\_writer just creates the bar=(value) method, no bar() method to  
> access the attribute.

Thanks! for clearing the doubt with an example. In general I thought;  
if varialbe can be writable then it should hold property of  
readable… which is not true in attr\_writer.

---

<div class="post-metadata">

**Author:** ![ycsunil](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/6f9a4e/32.png) [@ycsunil](https://www.ruby-forum.com/u/ycsunil)\
**Post date:** [February 26, 2008, 12:11pm UTC](https://www.ruby-forum.com/t/attr-writer-vs-attr-accessor/130194/5 "2008-02-26T12:11:58Z")

</div>

On Tue, Feb 26, 2008 at 11:09 AM, ycsunil [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Did you try it? I get an error, when I use the following  
> > from (irb):7  
> > irb(main):008:0\>
> > 
> > attr\_writer just creates the bar=(value) method, no bar() method to  
> > access the attribute.
> 
> Thanks! for clearing the doubt with an example. In general I thought;  
> if varialbe can be writable then it should hold property of  
> readable… which is not true in attr\_writer.

attr\_xxx do not “declare” any property or type of property.  
They just generate methods.  
attr\_reader :xxx generates a method that returns the value of the  
instance variable @xxx  
attr\_writer :xxx generates a method xxx= that sets the value of the  
instance variable @xxx  
attr\_accessor :xxx generates the two above methods.

Hope this helps,

Jesus.
