# Attr\_writer vs attr\_accessor

**URL:** <https://rubytalk.org/t/attr-writer-vs-attr-accessor/44772>\
**Category:** ruby-talk\
**Created:** [26 February 2008 08:29 UTC](https://rubytalk.org/t/attr-writer-vs-attr-accessor/44772 "2008-02-26T08:29:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sunil\_Chikkegowda](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sunil\_Chikkegowda](https://rubytalk.org/u/Sunil_Chikkegowda)\
**Post date:** [26 February 2008 08:29 UTC](https://rubytalk.org/t/attr-writer-vs-attr-accessor/44772/1 "2008-02-26T08:29:55Z")

</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:** ![Thomas\_Wieczorek](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@Thomas\_Wieczorek](https://rubytalk.org/u/Thomas_Wieczorek)\
**Post date:** [26 February 2008 09:38 UTC](https://rubytalk.org/t/attr-writer-vs-attr-accessor/44772/2 "2008-02-26T09:38:01Z")

</div>

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"\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):7  
irb(main):008:0\>

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

> **···**
>
> On Tue, Feb 26, 2008 at 9:29 AM, ycsunil \<ycsunil@gmail.com\> 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?

---

<div class="post-metadata">

**Author:** ![7stud](https://avatars.discourse-cdn.com/v4/letter/7/57b2e6/32.png) [@7stud](https://rubytalk.org/u/7stud)\
**Post date:** [26 February 2008 09:44 UTC](https://rubytalk.org/t/attr-writer-vs-attr-accessor/44772/3 "2008-02-26T09:44:38Z")

</div>

Sunil Chikkegowda wrote:

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

Prove it.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sunil\_Chikkegowda](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@Sunil\_Chikkegowda](https://rubytalk.org/u/Sunil_Chikkegowda)\
**Post date:** [26 February 2008 10:09 UTC](https://rubytalk.org/t/attr-writer-vs-attr-accessor/44772/4 "2008-02-26T10:09:57Z")

</div>

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.

> **···**
>
> On Feb 26, 2:38 pm, Thomas Wieczorek \<wieczo...@googlemail.com\> wrote:
> 
> > On Tue, Feb 26, 2008 at 9:29 AM, ycsunil \<ycsu...@gmail.com\> 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:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [26 February 2008 11:10 UTC](https://rubytalk.org/t/attr-writer-vs-attr-accessor/44772/5 "2008-02-26T11:10:50Z")

</div>

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.

> **···**
>
> On Tue, Feb 26, 2008 at 11:09 AM, ycsunil \<ycsunil@gmail.com\> wrote:
> 
> > On Feb 26, 2:38 pm, Thomas Wieczorek \<wieczo...@googlemail.com\> wrote:  
> > \> On Tue, Feb 26, 2008 at 9:29 AM, ycsunil \<ycsu...@gmail.com\> 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.
> > 
> > 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.
