Afficher un message
Vieux 14/09/2007, 22h21   #2
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: metaprogramming to generate list of class-specific attributes

Phlip wrote:
...
> class Kozmik
> def self.acts_as_frog(species)
> @frog_list ||= []
> @frog_list << species
> self.class.send :attr_accessor, :frog_list
> end
> end
>
> Is that as clean and lean as it could be? Is there some way to do this with
> even fewer lines?
>
> Next question: Couldn't class Kozmik be a module instead?


Sure...

module Kozmik
def acts_as_frog(species)
@frog_list ||= []
@frog_list << species
self.class.send :attr_accessor, :frog_list
end
end

class Toad; extend Kozmik
...
end

Or is that cheating somehow?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
 
Page generated in 0,04593 seconds with 9 queries