Automatically setting attributes

Do those methods have any advantages over doing

@goo, @moo = 0, 0

?

···

-----Original Message-----
From: David A. Black [mailto:dblack@wobblini.net]
Sent: Wednesday, July 28, 2004 7:39 PM
To: ruby-talk ML
Subject: Re: Automatically setting attributes

Hi --

On Thu, 29 Jul 2004, Mehr, Assaph (Assaph) wrote:

>
> > How can I do something like the following:
>
> > class Foo
> > attr_accessor :goo, :moo
> > def initalize
> > @attr.each { | attrib| attrib = 0 }
>
> [:goo,:moo].each { |sym| send "#{sym}=", 0 }
> or:
> [:goo,:moo].each { |sym| instance_eval "@#{sym} ||= 0" }
>
> > end
> > end

You could also use: instance_variable_set("@#{sym}",0)
(which might be more self-documenting).

David

Hi --

···

On Thu, 29 Jul 2004, Laughlin, Joseph V wrote:

> -----Original Message-----
> From: David A. Black [mailto:dblack@wobblini.net]
> Sent: Wednesday, July 28, 2004 7:39 PM
> To: ruby-talk ML
> Subject: Re: Automatically setting attributes
>
>
> Hi --
>
> On Thu, 29 Jul 2004, Mehr, Assaph (Assaph) wrote:
>
> >
> > > How can I do something like the following:
> >
> > > class Foo
> > > attr_accessor :goo, :moo
> > > def initalize
> > > @attr.each { | attrib| attrib = 0 }
> >
> > [:goo,:moo].each { |sym| send "#{sym}=", 0 }
> > or:
> > [:goo,:moo].each { |sym| instance_eval "@#{sym} ||= 0" }
> >
> > > end
> > > end
>
> You could also use: instance_variable_set("@#{sym}",0)
> (which might be more self-documenting).
>
>
> David
>

Do those methods have any advantages over doing

@goo, @moo = 0, 0

?

As Assaph said, not really if you know the names in advance. I hadn't
really picked up on that. But I don't mind putting in a good word for
#instance_variable_set :slight_smile:

David

--
David A. Black
dblack@wobblini.net

It's always annoyed me that you need to specify the '@' when using
#instance_variable_set. Shouldn't it be implicit?

  instance_variable_set :foo, 5

Gavin

···

On Thursday, July 29, 2004, 1:07:21 PM, David wrote:

But I don't mind putting in a good word for #instance_variable_set
:slight_smile: