[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

attr_writer vs attr_accessor

ycsunil

2/26/2008 8:27:00 AM

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
4 Answers

Thomas Wieczorek

2/26/2008 9:38:00 AM

0

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?
>

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.

7stud --

2/26/2008 9:45:00 AM

0

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-....

ycsunil

2/26/2008 10:07:00 AM

0

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.

Jesús Gabriel y Galán

2/26/2008 11:11:00 AM

0

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.

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.