<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>PaulBarry.com - Calling Methods During Class Definition</title>
  <subtitle type="html">My thoughts, ideas, questions and concerns on technology, sports, music and life</subtitle>
  <id>tag:paulbarry.com,2007:Paulbarry.com</id>
  <generator uri="http://www.paulbarry.com" version="3.0">PaulBarry.com</generator>
  <link href="http://paulbarry.com/xml/atom/article/4882/feed.xml" rel="self" type="application/atom+xml"/>
  <link href="http://paulbarry.com/articles/2008/04/17/calling-methods-during-class-definition" rel="alternate" type="text/html"/>

  <updated>2008-11-04T07:37:08-05:00</updated>
  <entry>
    <author>
      <name>Paul Barry</name>
      <email>mail@paulbarry.com</email>
    </author>
    <id>urn:uuid:d0f57e92-fab5-41e4-b7c2-a825ac7b88cd</id>

    <published>2008-04-17T21:38:31-04:00</published>
    <updated>2008-04-17T21:38:31-04:00</updated>
    <title type="html">Calling Methods During Class Definition</title>
    <link href="http://paulbarry.com/articles/2008/04/17/calling-methods-during-class-definition" rel="alternate" type="text/html"/>

    <category term="technology" scheme="http://paulbarry.com/articles/category/technology" label="Technology"/>
        <category term="Python" scheme="http://paulbarry.com/articles/tag/python"/>
    <category term="PHP" scheme="http://paulbarry.com/articles/tag/php"/>
    <category term="groovy" scheme="http://paulbarry.com/articles/tag/groovy"/>
    <category term="Ruby" scheme="http://paulbarry.com/articles/tag/ruby"/>
    <category term="Java" scheme="http://paulbarry.com/articles/tag/java"/>
        <summary type="html">&lt;p&gt;One of the features I like most about Ruby is the ability to execute code during the definition of a class.  Here&apos;s a simple example:  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Foo
  puts &quot;hi&quot;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A more useful example is that you can call a method and self is set to the class you are defining.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Foo
  def self.whoami
    puts &quot;You are #{self}&quot;
  end
end
class Bar &amp;lt; Foo
  whoami
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This prints &lt;code&gt;You are Bar&lt;/code&gt;.  This is a feature that isn&apos;t common to most languages, for example, you can&apos;t do it in PHP, Python(I don&apos;t think, Pythonistas jump in there if I&apos;m wrong), Java or even Groovy.  The reason why this kind of method is so helpful is this how you can write code that writes code.  This is the closest thing Ruby has to Lisp macros.  You see this used in Ruby in with the &lt;code&gt;attr_accessor&lt;/code&gt; method and in Rails with many methods, &lt;code&gt;belongs_to&lt;/code&gt; and &lt;code&gt;has_many&lt;/code&gt; being the most obvious examples.&lt;/p&gt;

&lt;p&gt;This allows you to define the metadata about a class in the most DSL-like syntax.  It would be really great to see this kind of thing in Groovy.&lt;/p&gt;
</summary>
    <content type="html">&lt;p&gt;One of the features I like most about Ruby is the ability to execute code during the definition of a class.  Here&apos;s a simple example:  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Foo
  puts &quot;hi&quot;
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A more useful example is that you can call a method and self is set to the class you are defining.  &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Foo
  def self.whoami
    puts &quot;You are #{self}&quot;
  end
end
class Bar &amp;lt; Foo
  whoami
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This prints &lt;code&gt;You are Bar&lt;/code&gt;.  This is a feature that isn&apos;t common to most languages, for example, you can&apos;t do it in PHP, Python(I don&apos;t think, Pythonistas jump in there if I&apos;m wrong), Java or even Groovy.  The reason why this kind of method is so helpful is this how you can write code that writes code.  This is the closest thing Ruby has to Lisp macros.  You see this used in Ruby in with the &lt;code&gt;attr_accessor&lt;/code&gt; method and in Rails with many methods, &lt;code&gt;belongs_to&lt;/code&gt; and &lt;code&gt;has_many&lt;/code&gt; being the most obvious examples.&lt;/p&gt;

&lt;p&gt;This allows you to define the metadata about a class in the most DSL-like syntax.  It would be really great to see this kind of thing in Groovy.&lt;/p&gt;
</content>
  </entry>
  </feed>