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
|