# Exploring Metaprogramming

**URL:** <https://rubytalk.org/t/exploring-metaprogramming/30616>\
**Category:** ruby-talk\
**Created:** [4 September 2006 02:39 UTC](https://rubytalk.org/t/exploring-metaprogramming/30616 "2006-09-04T02:39:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Gorsuch](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Michael\_Gorsuch](https://rubytalk.org/u/Michael_Gorsuch)\
**Post date:** [4 September 2006 02:39 UTC](https://rubytalk.org/t/exploring-metaprogramming/30616/1 "2006-09-04T02:39:50Z")

</div>

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](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  
&nbsp;&nbsp;&nbsp;traits :life  
&nbsp;&nbsp;&nbsp;life 100

&nbsp;&nbsp;&nbsp;command :fly

&nbsp;&nbsp;&nbsp;def fly  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "I am flying..."  
&nbsp;&nbsp;&nbsp;end

end

After an instance is created, it will contain the instance variable  
'commands', which is an array holding the symbol '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? 😉

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [4 September 2006 03:24 UTC](https://rubytalk.org/t/exploring-metaprogramming/30616/2 "2006-09-04T03:24:19Z")

</div>

> 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](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  
> &nbsp;&nbsp;traits :life  
> &nbsp;&nbsp;life 100
> 
> &nbsp;&nbsp;command :fly
> 
> &nbsp;&nbsp;def fly  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "I am flying..."  
> &nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;def self.command(com)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@commands ||=   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@commands |= [com]  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def self.commands  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@commands ||=   
&nbsp;&nbsp;&nbsp;end  
end

class Dragon \< Creature  
&nbsp;&nbsp;&nbsp;command :fly  
end

p Dragon.commands #=\> [:fly]

> **···**
>
> On Sep 3, 2006, at 10:39 PM, Michael Gorsuch wrote:
> 
> > 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? 😉

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [4 September 2006 03:32 UTC](https://rubytalk.org/t/exploring-metaprogramming/30616/3 "2006-09-04T03:32:24Z")

</div>

Sorry I seem to have under estimated what you wanted.

try this instead:

% cat dragon.rb  
class Creature  
&nbsp;&nbsp;&nbsp;def self.command(cmd)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old\_initialize = instance\_method(:initialize)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:initialize) do |\*args|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@commands ||=   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@commands |= [cmd]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old\_initialize.bind(self).call(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;def commands  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@commands ||=   
&nbsp;&nbsp;&nbsp;end  
end

class Dragon \< Creature  
&nbsp;&nbsp;&nbsp;command :fly  
&nbsp;&nbsp;&nbsp;def fly  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "I'm flying"  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;command :breathe\_fire

&nbsp;&nbsp;&nbsp;def breathe\_fire  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "I'm breathing fire!"  
&nbsp;&nbsp;&nbsp;end  
end

dragon = Dragon.new  
p dragon.commands

% ruby dragon.rb  
[:breathe\_fire, :fly]

> **···**
>
> On Sep 3, 2006, at 10:39 PM, Michael Gorsuch wrote:
> 
> > 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  
> > &nbsp;&nbsp;traits :life  
> > &nbsp;&nbsp;life 100
> > 
> > &nbsp;&nbsp;command :fly
> > 
> > &nbsp;&nbsp;def fly  
> > &nbsp;&nbsp;&nbsp;&nbsp;puts "I am flying..."  
> > &nbsp;&nbsp;end
> > 
> > end
> > 
> > After an instance is created, it will contain the instance variable  
> > 'commands', which is an array holding the symbol '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? 😉

---

<div class="post-metadata">

**Author:** ![Michael\_Gorsuch](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Michael\_Gorsuch](https://rubytalk.org/u/Michael_Gorsuch)\
**Post date:** [4 September 2006 03:56 UTC](https://rubytalk.org/t/exploring-metaprogramming/30616/4 "2006-09-04T03:56:52Z")

</div>

Thanks a ton for this! This is amazing!

> **···**
>
> On 9/3/06, Logan Capaldo \<logancapaldo@gmail.com\> wrote:
> 
> > On Sep 3, 2006, at 10:39 PM, Michael Gorsuch wrote:
> > 
> > \>  
> > \> 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'.  
> > \>  
> > \> 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? 😉
> > 
> > Sorry I seem to have under estimated what you wanted.
> > 
> > try this instead:
> > 
> > % cat dragon.rb  
> > class Creature  
> > &nbsp;&nbsp;&nbsp;def self.command(cmd)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old\_initialize = instance\_method(:initialize)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(:initialize) do |\*args|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@commands ||=   
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@commands |= [cmd]  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;old\_initialize.bind(self).call(\*args)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;def commands  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@commands ||=   
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > class Dragon \< Creature  
> > &nbsp;&nbsp;&nbsp;command :fly  
> > &nbsp;&nbsp;&nbsp;def fly  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "I'm flying"  
> > &nbsp;&nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;&nbsp;command :breathe\_fire
> > 
> > &nbsp;&nbsp;&nbsp;def breathe\_fire  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "I'm breathing fire!"  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > dragon = Dragon.new  
> > p dragon.commands
> > 
> > % ruby dragon.rb  
> > [:breathe\_fire, :fly]

---

<div class="post-metadata">

**Author:** ![Ben\_Nagy](https://avatars.discourse-cdn.com/v4/letter/b/8491ac/32.png) [@Ben\_Nagy](https://rubytalk.org/u/Ben_Nagy)\
**Post date:** [4 September 2006 05:53 UTC](https://rubytalk.org/t/exploring-metaprogramming/30616/5 "2006-09-04T05:53:59Z")

</div>

[...]  
[...]

> \> class Creature  
> \> def self.command(cmd)  
> \> old\_initialize = instance\_method(:initialize)  
> \> define\_method(:initialize) do |\*args|  
> \> @commands ||=   
> \> @commands |= [cmd]  
> \> old\_initialize.bind(self).call(\*args)  
> \> end  
> \> end  
> \>  
> \> def commands  
> \> @commands ||=   
> \> end  
> \> end  
> \>  
> \> class Dragon \< Creature  
> \> command :fly  
> \> def fly  
> \> puts "I'm flying"  
> \> end

[...]

I'm doing a very similar thing, but my approach was different. I have  
something vaguely like:

class Creature

&nbsp;&nbsp;class \<\< self; attr\_reader :commands end

&nbsp;&nbsp;def self.command( cmd )  
&nbsp;&nbsp;&nbsp;&nbsp;@commands ||=   
&nbsp;&nbsp;&nbsp;&nbsp;@commands |= [cmd]  
&nbsp;&nbsp;end  
&nbsp;&nbsp;[...]  
&nbsp;&nbsp;def initialize( \*args )  
&nbsp;&nbsp;&nbsp;&nbsp;self.class.commands.each do |command| #do something  
&nbsp;&nbsp;[...or maybe even...]  
&nbsp;&nbsp;&nbsp;&nbsp;@commands=self.class.commands

Which kind of puts a sticky note on the fridge of the derived class that it  
needs to do something, but allows you to put off what you're doing until  
you're actually instantiating the object. This way when you get to #do  
something, you can do it based on the arguments passed at initialize time,  
like say you get dragon=Dragon.new(AmazinglyPowerful) - you can make your  
breathe\_fire command hot enough to melt sand.

I have also actually been passing code as blocks:

Class Dragon \< Creature  
&nbsp;&nbsp;command :fly, proc {puts "Lookit me, Mom!"}  
end

Which lets you instance\_eval the block later in the correct namespace, or  
just define the method at runtime with define\_method. That can also be used  
in combination with a trick I found here on ruby-lang via google to  
instance\_eval a block with arguments; said trick will soon not be neccesary,  
so I'll just add a note to google instance\_exec and you'll probably dig it  
up fine.

Anyway, I really like the line above that rebinds the initialize method to  
perform a kind of 'reverse super'...

old\_initialize.bind(self).call(\*args)

...and I'm trying to work out if it provides some benefits I have  
overlooked, apart from the fact that it will make the initialize method look  
a little cleaner and the things like self.command look a little messier.

Cheers,

ben

> **···**
>
> > -----Original Message-----  
> > On 9/3/06, Logan Capaldo \<logancapaldo@gmail.com\> wrote:
