Creating objects from a template

Lähettäjä: nobu.nokada@softhome.net
Aihe: Re: Creating objects from a template

Hi,

At Wed, 9 Feb 2005 15:20:07 +0900,
David McCabe wrote in [ruby-talk:130214]:
> In python, my parser returns a hash of unit_type objects, where the
> names of units are the keys. Each time a unit is instantiated, it
> receives a unit_type object as an argument. Now here's the interesting
> part: it copies the instance variables from the unit_type into itself.
>
> class Unit(object):
> def __init__(self, type):
> self.type = type
> self.__dict__.update(self.type.__dict__)
>
> There's more to it than that, but that's the interesting part. So now
> I can say:
>
> my_new_unit = Unit(types["Panzer"])
>
> And I'll get a Unit object with all the values for Panzers
> initialized.

What about:

  module Unit
  end

  my_new_unit = types["Panzer"].new.extend(Unit) # As later

I thought #extend only works for instance methods, not variables?

Presumably it'd be OK if Unit defined accessors for all its IVs,
but then those wouldn't have initial values (unless one built them
in the accessors...)

--
Nobu Nakada

E

Hi,

At Sat, 12 Feb 2005 13:16:23 +0900,
E S wrote in [ruby-talk:130578]:

> my_new_unit = types["Panzer"].new.extend(Unit) # As later

I thought #extend only works for instance methods, not variables?

extend_object module method will be called.

  module Unit
    def self.extend_object(obj)
      super
      obj._unit_initialize
    end
  end

···

--
Nobu Nakada