<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>John Yerhot - Weblog &#187; Metaprogramming</title>
	<atom:link href="http://www.johnyerhot.com/tag/metaprogramming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnyerhot.com</link>
	<description>moof</description>
	<lastBuildDate>Mon, 16 Aug 2010 22:11:50 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple Meta Programming in Ruby</title>
		<link>http://www.johnyerhot.com/2008/08/10/meta-programming-in-ruby/</link>
		<comments>http://www.johnyerhot.com/2008/08/10/meta-programming-in-ruby/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 20:58:24 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[define_method]]></category>
		<category><![CDATA[Metaprogramming]]></category>

		<guid isPermaLink="false">http://www.johnyerhot.com/?p=84</guid>
		<description><![CDATA[Metaprogramming.  What a lovely buzz word.  I guess I&#8217;ve heard it enough and knew what the short definition is.  Metaprogramming is code that writes code.  I think it is one of those things I just never thought about, even though I had used concepts and even written some before without realizing it until recently.
Here [...]]]></description>
			<content:encoded><![CDATA[<p><em><a href="http://en.wikipedia.org/wiki/Metaprogramming">Metaprogramming</a></em>.  What a lovely buzz word.  I guess I&#8217;ve heard it enough and knew what the short definition is.  Metaprogramming is <em>code that writes code</em>.  I think it is one of those things I just never thought about, even though I had used concepts and even written some before without realizing it until recently.</p>
<p>Here is a short and simple example.</p>
<pre>class Something

@my_hash = {"foo" =&gt; "1234", "bar" =&gt; "5678"}

def initialize
  @my_hash.each do |a, b|
       self.instance_eval do
             define_method(a.to_s) {b.to_s}
       end
  end
end
end
</pre>
<p>And now we can play with it.</p>
<pre>a = Something.new
a.foo  # =&gt; 1234
a.bar # =&gt; 5678
</pre>
<p>Now, what happened here is pretty neat in my opinion.  We took our hash, my_hash, and in our initialize method, created two instance methods from its values.  <a href="http://www.ruby-doc.org/core/classes/Module.html#M001677">define_method</a> is what did all the magic.  You need to pass it a proc or, like what I did, just give it a simple block (just the string value from my hash).</p>
<p>Pretty neat, eh?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnyerhot.com/2008/08/10/meta-programming-in-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
