Automatically setting attributes

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 have to specify the variable names. Ruby only creates an instance of
the variable once it's accessed.

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

--
David A. Black
dblack@wobblini.net