<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Aaron Feng: Method inheritance?</title>
    <link>http://www.aaronfeng.com/articles/2007/03/12/method-inheritance</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Adventures in software development</description>
    <item>
      <title>Method inheritance?</title>
      <description>&lt;p&gt;A couple of days ago I was poking around &lt;a href="http://code.whytheluckystiff.net/parkplace/"&gt;Park Place&lt;/a&gt; source code and I saw something resembling the following syntax:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;# ... is some arbitrary string in this case&lt;/span&gt;
&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;A&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;B&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;...&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
 &lt;span class="comment"&gt;# ...&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I am no &lt;a href="http://www.ruby-lang.org/en/"&gt;Ruby&lt;/a&gt; expert, in fact, I have written very little Ruby code so far.  However, the code above looked really odd to me.  It looks like class &lt;strong&gt;A&lt;/strong&gt; is inheriting from class &lt;strong&gt;B&lt;/strong&gt;, and class &lt;strong&gt;B&lt;/strong&gt; is taking some arbitrary string as an argument.  &lt;/p&gt;

&lt;p&gt;Well, after closer inspection I was completely wrong on what &lt;strong&gt;B&lt;/strong&gt; is.  &lt;strong&gt;B&lt;/strong&gt; is not a class at all, in fact, it is a method under cover.  &lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;B&lt;/span&gt; &lt;span class="ident"&gt;str&lt;/span&gt;
 &lt;span class="comment"&gt;# do something with str variable here&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;'...' is some argument to the &lt;strong&gt;B&lt;/strong&gt; method, which makes a lot of sense, but how can class &lt;strong&gt;A&lt;/strong&gt; inherit from a method?  If you try the code above, whatever is in method &lt;strong&gt;B&lt;/strong&gt; will execute, but you will get an error when class &lt;strong&gt;A&lt;/strong&gt; is trying to inherit from &lt;strong&gt;B&lt;/strong&gt;.  The reason is simple, class &lt;strong&gt;A&lt;/strong&gt; is expecting to inherit from a class not a method.  To prevent the error, you need to somehow construct a class in method &lt;strong&gt;B&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;B&lt;/span&gt; &lt;span class="ident"&gt;str&lt;/span&gt;
 &lt;span class="comment"&gt;# do something with str variable here&lt;/span&gt;
 &lt;span class="constant"&gt;Class&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Ruby automatically returns the last line as the return value.  In this case, you will not get the error anymore.&lt;/p&gt;

&lt;p&gt;The first question one should ask is, why would anyone want to do this?  The code I showed above is a very simple version of the real code that appeared in Park Place.  In Park Place it creates a new class and adds methods to the class based on the input.  This is powerful because it allows one to hide the base class since it does not actually exist.  On top of that, using this technique one can write code that writes code.  &lt;a href="http://www.rubyonrails.org/"&gt;Ruby on Rails&lt;/a&gt; uses similar techniques to achieve all the magic.  &lt;/p&gt;</description>
      <pubDate>Mon, 12 Mar 2007 22:05:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:f948ef92-4ee9-42ff-852e-f6178bf7030a</guid>
      <author>Aaron Feng</author>
      <link>http://www.aaronfeng.com/articles/2007/03/12/method-inheritance</link>
      <category>programming</category>
    </item>
    <item>
      <title>"Method inheritance?" by Aredridel</title>
      <description>&lt;p&gt;Camping uses it for its route classes -- when the R method is called, it registers an anonymous class with some special methods (used to associate the string with the class) and puts it in a global routing table.&lt;/p&gt;

&lt;p&gt;class Index &amp;lt; R '/'
end&lt;/p&gt;

&lt;p&gt;Creates a superclass for Index (which then catches the subclassing operation and ties it all together), and associates it with the URL "/"&lt;/p&gt;</description>
      <pubDate>Mon, 12 Mar 2007 23:12:57 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:c7725757-524b-4fca-ac24-1a995c98e851</guid>
      <link>http://www.aaronfeng.com/articles/2007/03/12/method-inheritance#comment-15</link>
    </item>
  </channel>
</rss>
