On Sep 3, 2006, at 10:39 PM, Michael Gorsuch wrote: > I've got some downtime this weekend, so I'm pooring over various > examples of > metaprogramming w/ Ruby. > > I really like _why's example regarding Dwemthy, but am curious as > to how to > achieve my end. > > The article in question is here: > > http://poignantguide.net/ruby/chapter-6.html#section3 > > _why's method allows us to build 'traits' for the character (by > calling the > 'trait' class method), and the meta programming behind the scenes > builds an > 'initialize' instance method for the class. > > What if I want to build a second method called 'command'? It > should place > the argument in an array that can be referenced later. It will > need to also > build an initialize method for my class, but one of them will have > to be > overwritten. > > Does anyone have any smart ways to combine or chain these two > initialize > methods together? > > I ultimately want to do something like this: > > class Dragon < Creature > traits :life > life 100 > > command :fly > > def fly > puts "I am flying..." > end > > end > > After an instance is created, it will contain the instance variable > 'commands', which is an array holding the symbol 'fly'. > here's something to get you started: class Creature def self.command(com) @commands ||= [] @commands |= [com] end def self.commands @commands ||= [] end end class Dragon < Creature command :fly end p Dragon.commands #=> [:fly] > Does anyone have any ideas? In the end, I'd be adding more and > more of > these commands to my DSL. This is a DSL, right? ;-)