<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.2.3" -->
<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/"
	>

<channel>
	<title>Klank Boom Klang</title>
	<link>http://www.klankboomklang.com</link>
	<description>Ruby and otherwise by Patrick Farley</description>
	<pubDate>Mon, 24 Mar 2008 23:57:47 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>
	<language>en</language>
			<item>
		<title>Modules Part II - Strange Brew</title>
		<link>http://www.klankboomklang.com/2008/02/04/modules-part-ii-strange-brew/</link>
		<comments>http://www.klankboomklang.com/2008/02/04/modules-part-ii-strange-brew/#comments</comments>
		<pubDate>Tue, 05 Feb 2008 03:23:06 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[patterns/idiom/style]]></category>

		<category><![CDATA[Ruby Internals]]></category>

		<category><![CDATA[MRI]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2008/02/04/modules-part-ii-strange-brew/</guid>
		<description><![CDATA[This post continues to look at Ruby Modules and their properties.
It&#8217;s possible to mix a Module into a second Module which can then be mixed into a class.  The result is that an entire Module chain is moved into the a classes inheritance chain with one call to include.

module OneStrand
end

module TwoStrand
  include OneStrand
end

class [...]]]></description>
			<content:encoded><![CDATA[<p>This post <a href="http://www.klankboomklang.com/2007/11/25/modules-part-i-enter-the-include-class/">continues</a> to look at Ruby Modules and their properties.</p>
<p>It&#8217;s possible to mix a Module into a second Module which can then be mixed into a class.  The result is that an entire Module chain is moved into the a classes inheritance chain with one call to include.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
module OneStrand
end

module TwoStrand
  include OneStrand
end

class Web
  include TwoStrand
end

puts Web.ancestors.inspect
# => [Web, TwoStrand, OneStrand, Object, Kernel]
</textarea>
<p>Although this sort of mixin chaining is generally a bad practice which can lead to overly complex inheritance chains, it&#8217;s important in understanding how include works.  <b><b>rb_include_module</b></b> is a complex function.  As a first pass at it we&#8217;ll only look at the basic include loop&#8230;</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">
void
rb_include_module(klass, module)
    VALUE klass, module;
{
    VALUE p, c;
    c = klass;
    while (module) {
	    c = RCLASS(c)->super = include_class_new(module, RCLASS(c)->super);
	    module = RCLASS(module)->super;
    }
}
</textarea>
<p>The variable <b>c</b> here is used to store the current point of insertion.  The first time through the loop <b>c</b> points to the <b>klass</b> passed into the function.<br />
In the Web example above <b>c</b> is set to Web and <b>module</b> is TwoStrand before entering the while loop.  An include class is created for TwoStrand with a super pointer to Web&#8217;s super, Object.  Web&#8217;s super is set to TwoStrand.  Since any additional Modules to be inserted should be inserted above TwoStrand, the point of insertion, <b>c</b>, is set to the newly created TwoStrand include class.  On the next line <b>module</b> is set to TwoStrand&#8217;s super, OneStrand.  The next time through the loop a new include class is created for TwoStrand and it&#8217;s super is set to the super of the OneStrand include class, Object.  The OneStrand include class&#8217;s super is updated to point to the new TwoStrand include class and once again the point of insertion is moved.  Eventually super returns a null pointer and the loop exits.</p>
<p>An interesting property of the include class architecture is that an include class does not keep track of changes to the inheritance chain of the Module it &#8220;wraps&#8221;.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
module Strand
end

module Web
  include Strand
end

module Tangled
end

module Strand
  include Tangled
end

puts Strand.ancestors.inspect
# => [Strand, Tangled]

puts Web.ancestors.inspect
# => [Web, Strand]
</textarea>
<p>Although Strand&#8217;s super now points to Tangled, the Include class created for Strand when it was mixed into Web is unaffected by the change.  This is known as the <a href="http://eigenclass.org/hiki/The+double+inclusion+problem">Dynamic Module Include Problem</a>.  This makes sense if one bears in mind that the Strand Module in Web&#8217;s inheritance chain is actually an Include class.  Inclusion of Tangled within the Strand Module means that the the super pointer of Strand now points to an Include class for Tangled.  Any classes or Modules that includes Strand from here out will pull a new Include class that points to Strand&#8217;s Tangled Include class into their inheritance chain when <b>rb_include_module</b>.  Existing Include classes for Strand however remain blissfully unaware that their &#8220;real&#8221; Module now has an altered inheritance chain.</p>
<p>Part of the code that we did not look at in our first pass through rb_include_module is responsible for ensuring a Module is only included once in a classes inheritance chain.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Web
  include OneStrand
end

puts Web.ancestors.inspect
# => [Web, TwoStrand, OneStrand, Object, Kernel]
</textarea>
<p>Note that OneStrand was not inserted twice and did not change it&#8217;s position in the inheritance hierarchy.  Let&#8217;s add some code back into <b>rb_include_module</b> to see how this is implemented.</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">
void
rb_include_module(klass, module)
    VALUE klass, module;
{
    VALUE p, c;
 
    c = klass;
    while (module) {

	for (p = RCLASS(klass)->super; p; p = RCLASS(p)->super) {
		if (RCLASS(p)->m_tbl == RCLASS(module)->m_tbl) {
		    goto skip;
		}
	}
	  c = RCLASS(c)->super = include_class_new(module, RCLASS(c)->super);
      skip:
	    module = RCLASS(module)->super;
    }
}
</textarea>
<p>A for loop has been introduced inside of the while loop.  For each Module in <b>module</b>&#8217;s super chain the inheritance chain of <b>klass</b> is analyzed looking for a class whose method table is the same as the current Module&#8217;s method table.  This method table comparison is a slightly non-intuitive way of checking if the current inheritance chain includes either the actual Module being considered, or an include class mapping to that Module.  In either case the method tables would be the same, and would indicate that this Module is already included.  If so,  the include class creation is skipped and the while loop continues to the next Module being considered for inclusion.  Note that this means that while a Module may only be included once, that does not mean that a re-include will not have side effects.  Specifically, new additions to the Modules inheritance chain will be picked up by a re-include. </p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Web
  include TwoStrand
end

puts Web.ancestors.inspect
# => [Web, TwoStrand, Tangled, OneStrand, Object, Kernel]
</textarea>
<p>The TwoStrand Module is skipped over because its method table pointer is the same as the existing include class in Web&#8217;s super chain.  When super is called on TwoStrand this time around, Tangled is returned.  Since Tangled isn&#8217;t currently part of Web&#8217;s inheritance chain, a new include class is created for it and inserted immediately above TwoStrand.</p>
<p>It is possible for a Module to be included into an inheritance chain twice if it included into a super class after it has been included into a subclass.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
module Doppelganger
end

class Craftsman
end

class Blacksmith < Craftsman
  include Doppelganger
end

class Craftsman
  include Doppelganger
end

puts Blacksmith.ancestors.inspect
# => [Blacksmith, Doppelganger, Craftsman, Doppelganger, Object, Kernel]
</textarea>
<p>Blacksmith&#8217;s super points to a Doppelganger Include class which points to Craftsman at the time Doppelganger is included into Craftsman.  Craftsman has no knowledge of what classes may point to it and gamely allows its super to be set to a new Include class for Doppelganger.  It&#8217;s hard to imagine a situation where you would want this behavior, but understanding how it can happen may help debug a bizarre situation.</p>
<p>The next (and last post) on Modules will cover extend and class methods.  As always, thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2008/02/04/modules-part-ii-strange-brew/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Modules Part I - Enter the Include Class</title>
		<link>http://www.klankboomklang.com/2007/11/25/modules-part-i-enter-the-include-class/</link>
		<comments>http://www.klankboomklang.com/2007/11/25/modules-part-i-enter-the-include-class/#comments</comments>
		<pubDate>Mon, 26 Nov 2007 03:50:56 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[Ruby Internals]]></category>

		<category><![CDATA[MRI]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2007/11/25/modules-part-i-enter-the-include-class/</guid>
		<description><![CDATA[Modules in Ruby serve two purposes.  One is to provide namespace scope for code organization seen in the syntax Test::Unit::TestCase, where TestCase is a class defined inside of the Unit module, which is in turn defined within the Test module.  The second purpose of modules is to provide a method for code reuse [...]]]></description>
			<content:encoded><![CDATA[<p>Modules in Ruby serve two purposes.  One is to provide namespace scope for code organization seen in the syntax Test::Unit::TestCase, where TestCase is a class defined inside of the Unit module, which is in turn defined within the Test module.  The second purpose of modules is to provide a method for code reuse which falls somewhere between abstract base classes and multiple inheritance.  This post explores the implementation and consequences of this second purpose.</p>
<p>A module which defines code that will be shared by other modules or classes is known as a Mixin.  Mixing a module into a class is generally done with a call to the include method.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
module Useful
  def share_me
    puts "working hard"
  end
end

class NeedALittleHelp
  include Useful
end

NeedALittleHelp.new.share_me

#=> working hard
</textarea>
<p>A call to the ancestors of NeedALittleHelp will confirm that Useful has been inserted into NeedALittleHelp&#8217;s inheritance chain </p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
puts NeedALittleHelp.ancestors.inspect
#=> [NeedALittleHelp, Useful, Object, Kernel]
</textarea>
<p>The inheritance chain might be drawn as&#8230;</p>
<p><center><img src="/images/module01.jpg"></center></p>
<p>An included module will always be inserted into the inheritance chain below a declared super class.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class SuperKlass
end

module MixinModule
end

class Klass<SuperKlass
  include MixinModule
end

puts Klass.ancestors.inspect
#=> [Klass, MixinModule, SuperKlass, Object, Kernel
</textarea>
<p>It&#8217;s worth pointing out that a single inheritance chain has been preserved.  The include method has simply manipulated Klass&#8217;s inheritance chain by inserting the MixinModule module between the class and it&#8217;s super&#8230;</p>
<p><center><img src="/images/module02.jpg"></center></p>
<p>It&#8217;s possible to mix multiple modules into a class as below.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
module AnotherMixin
end

class Klass<SuperKlass
  include MixinModule
  include AnotherMixin
end

puts Klass.ancestors.inspect
#=> [Klass, AnotherMixin, MixinModule, SuperKlass, Object, Kernel]
</textarea>
<p>Once again the Klass&#8217;s inheritance chain has been manipulated so that the super of Klass now points to AnotherMixin whose super points to MixinModule&#8230;<br />
<center><img src="/images/module03.jpg"></center></p>
<p>This raises the question of what occurs when these modules are included into a new class in reverse order&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class BizarroKlass
  include AnotherMixin
  include MixinModule
end

puts BizarroKlass.ancestors.inspect
#=> [BizarroKlass, MixinModule, AnotherMixin, Object, Kernel]
</textarea>
<p>Here the super of MixinModule is pointing to AnotherMixin.  This is odd behavior&#8230;</p>
<p><center><img src="/images/module04.jpg"></center></p>
<p>It would seem that the super pointer of a module can point to different places depending on context.  Clearly include cannot be altering the super pointer of the actual module it acts on, as the order in which a class includes the module can result in differing inheritance chains.  At the same time, it&#8217;s convenient for there to be only once actual instance of a module to allow for minimal overhead in updating the module&#8217;s method table.  Ruby solves this problem with something called an Include Class.  When a module is included into a class (or module), a new class is created on the fly.  The method table of this class is a pointer to the included modules method table.  This ensures that each class that includes a module has a &#8220;copy&#8221; of the module who&#8217;s super pointer is free for manipulation.  Since the method table is a pointer to the one true method table for the module, updates to the modules methods are shared between all &#8220;copies&#8221; of the module.  This module &#8220;copy&#8221; is an Include Class.  </p>
<p>The code below is a simplified version of the C function for creating a new include class.</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">

static VALUE
include_class_new(module, super)
     VALUE module, super;
{
     NEWOBJ(klass, struct RClass);
     OBJSETUP(klass, rb_cClass, T_ICLASS);
   
     klass->m_tbl = RCLASS(module)->m_tbl;
     klass->super = super;
     RBASIC(klass)->klass = module;
 
     return (VALUE)klass;
 }

</textarea>
<p>As described above a new object is created of type T_ICLASS which is the C struct for storing include classes.  A pointer to the method table of of the module is copied to the include class.  On the next line the super for the include class is set to the super class passed into the function as a parameter.  The class pointer for the new class is set to the module which has been copied.  This reference to the actual module is is useful for the case, as in the ancestor method call above, where the interpreter needs to know the module associated with the include class.  This does mean that, unlike other classes,  the class of an include class is not a metaclass.  We&#8217;ll discuss the implications of this when looking at including class methods.  A better diagram of the NeedALittleHelp inheritance chain might look like this&#8230;</p>
<p><center><img src="/images/module05.jpg"></center></p>
<p>The next post will continue to explore modules and includes class.  We&#8217;ll dig deeper into implementation and cover some of the nuances of inheritance chain manipulation.  As always thanks for reading and feel free to leave comments with questions, corrections, or haiku.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2007/11/25/modules-part-i-enter-the-include-class/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Class Methods Part IV - DSL&#8217;s</title>
		<link>http://www.klankboomklang.com/2007/11/16/class-methods-part-iv-dsls/</link>
		<comments>http://www.klankboomklang.com/2007/11/16/class-methods-part-iv-dsls/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 19:25:24 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[patterns/idiom/style]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2007/11/16/class-methods-part-iv-dsls/</guid>
		<description><![CDATA[This last post in a series on Ruby class methods focuses on Domain Specific Languages.
There are many different types of DSL&#8217;s.  The broadest classification, coined I believe by Martin Fowler, distinguishes between internal and external DSL&#8217;s.  External DSL&#8217;s are languages with a language specific compiler or interpreter.  If you are using YACC [...]]]></description>
			<content:encoded><![CDATA[<p>This last post in a series on Ruby class methods focuses on Domain Specific Languages.</p>
<p>There are many different types of DSL&#8217;s.  The broadest classification, coined I believe by <a href="http://martinfowler.com/bliki/DomainSpecificLanguage.html">Martin Fowler</a>, distinguishes between internal and external DSL&#8217;s.  External DSL&#8217;s are languages with a language specific compiler or interpreter.  If you are using YACC or ANTLR to define the grammar of your DSL, you&#8217;re writing an external DSL.  <a href="http://functionalform.blogspot.com/">Nathan Sobo</a> presented an interesting tool at RubyConf07 for defining language parsers and interpreters.  <a href="http://rubyforge.org/projects/treetop/">Treetop</a> allows you to create an external DSL using a grammar definition DSL and then easily define the interpreter in Ruby.  Here is a simple phone number parser&#8230;  </p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
grammar PhoneGrammar
  rule phone_number
    optional_open_paren area_code optional_closed_paren optional_delimiter exchange optional_delimiter station {
      def phone_number 
        area_code.text_value + exchange.text_value + station.text_value
      end
    }
  end

  rule optional_open_paren
    '(' ' '* / ''
  end

  rule optional_closed_paren
    ' '* ')' / ''
  end
  
  rule area_code
    number number number
  end

  rule exchange
    number number number
  end

  rule station
    number number number number
  end
  
  rule optional_delimiter
    '-' / '.' / [\s]*
  end
  
  rule number
    [0-9]
  end
end
</textarea>
<p>The really interesting bit is the <b>phone_number</b> method defined on the <b>phone_number</b> rule.  The ability to add methods to nodes allows for a lot of exciting interpreter possibilities.</p>
<p>The advantage of external DSL&#8217;s is unlimited expressiveness.  As long as you can define a parser for your language, you can express your intent in whatever syntax maps most closely to your domain.  An interesting example of an external DSL is ANSI SQL.  I&#8217;m not referring to the general purpose procedural monstrosities implemented by various vendors, but rather to the small language specific to the domain of set manipulation.  Another example is Make for the domain of software builds.  All that expressiveness comes at a high cost.  The big disadvantage of external DSL&#8217;s is that you have to define the grammar, build the parser, and build the interpreter/compiler.  If the author doesn&#8217;t define conditional statements, the language doesn&#8217;t have conditional statements, etc&#8230;  </p>
<p>Internal DSL&#8217;s are languages whose definitions mandate syntactically correct programs for a &#8220;hosting&#8221; general purpose language.  The hosting language&#8217;s interpreter or compiler is then responsible for program execution.  The advantages and disadvantages of internal DSL&#8217;s are an exact mirror to those of external DSL&#8217;s.  On the up side, because an internal DSL is defined within the context of an existing general purpose language, the language designer picks up a lot of functionality for free.  Someone has already written a parser and interpreter/compiler for the host language, and provided all the common features that one expects from a modern language.  This leaves the language designer free to focus on a compelling domain syntax for his or her little language.  There is of course a catch, the host language puts immutable restrictions on the expressiveness of an internal DSL.  This can be more or less of a problem depending on the natural expressiveness of the host language.  Ruby is a fairly expressive.  Syntactic features like optional braces, optional parenthesis, do end blocks and inline conditionals allow for creative authors to declare a fairly intuitive DSL syntax without violating Ruby grammar.</p>
<p>Within the field of internal DSL&#8217;s Ruby allows for a variety of styles.  <a href="http://weblog.jamisbuck.org/">Jamis Buck</a> has written an <a href="http://weblog.jamisbuck.org/2006/4/20/writing-domain-specific-languages">excellent introduction</a> to these styles.  We&#8217;ll focus here on the DSL&#8217;s style Jamis calls Class Macros.</p>
<p>The most prominent example of a class macro based DSL in Ruby is ActiveRecord&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Ninja<ActiveRecord::Base
  belongs_to :clan
  has_many :disguises
  validates_presence_of :back_story
end
</textarea>
<p>Some would argue that this sort of thing isn&#8217;t really a DSL at all.  The definition of &#8220;DSL&#8221; is very slippery, and subject to a debate that is as useless as it is endless.  I&#8217;ll use Jamis&#8217;s inclusion of it into the pantheon as my <a href="http://en.wikipedia.org/wiki/Appeal_to_authority">argumentum ad verecundiam.</a></p>
<p>This style of DSL is used to alter subclasses in an intuitive and domain expressive way.  In some cases this becomes nothing more than an extremely fancy implementation of the Template Method pattern, where the template methods are defined based on instructions communicated via the DSL.  In the extreme case, subclasses can be reduced to configuration files&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
require 'net/http'
module HttpGettable
  def site(url, options)
    define_method :get do

      uri = URI.parse(URI.escape(url))
      req = Net::HTTP::Get.new(uri.to_s)

      http = Net::HTTP.new(uri.host, uri.port)
      http.read_timeout = options[:timeout]
      http.open_timeout = options[:timeout]

      http.request(req)
    end
  end
end

class GoogleGetter
  extend HttpGettable
  site "http://www.google.com", :timeout=> 3
end

class YahooGetter
  extend HttpGettable
  site "http://www.yahoo.com", :timeout=> 5
end
</textarea>
<p>Note that HttpGettable is defined as a module not a base class.  In general, in Ruby, if you think you need an Abstract Base Class, it pays to consider if the relationship is really an <b>is a</b>, or can be better expressed as a <b>can</b>.  If it truly is an <b>is a</b> relationship, go ahead and create a Base class.  There has been a lot of discussion as to how one can force a class to be abstract in Ruby.  My rule of thumb is, unless someone might die if the class is instantiated, I&#8217;ll depend on a reasonable name (like AbstractGetter) to communicate the usage to other developers.  If you are developing pacemakers, you might want to define initialize to raise an exception.</p>
<p>HttpGettable could also be implemented using the configuration to define what Kent Beck calls Constant Methods&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
require 'net/http'
module HttpGettable
  def get 
    uri = URI.parse(URI.escape(url))
    req = Net::HTTP::Get.new(uri.to_s)

    http = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout, http.open_timeout = timeout
    http.request(req)
  end

  def self.included(klass)
    klass.extend ClassMethods
  end
  
  module ClassMethods
    def url(url)
      define_method :url, lambda { url }
    end

    def timeout(limit)
      define_method :timeout, lambda { limit }
    end
  end
end

class GoogleGetter
  include HttpGettable
  url "http://www.google.com"
  timeout 3
end

class YahooGetter
  include HttpGettable
  url "http://www.yahoo.com"
  timeout  5
end

</textarea>
<p>This implementation trades some of the metaprogramming nesting complexity in favor of a well known module include idiom.  It provides for an aesthetically pleasing syntax, but at the cost of some complexity.  One could also give up on DSL&#8217;s and use plain old constants&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
require 'net/http'
module HttpGettable
  def get 
    uri = URI.parse(URI.escape(self.class.const_get("URL")))
    req = Net::HTTP::Get.new(uri.to_s)

    http = Net::HTTP.new(uri.host, uri.port)
    http.read_timeout, http.open_timeout = self.class.const_get("TIMEOUT")
    http.request(req)
  end
end

class GoogleGetter
  include HttpGettable
  URL = "http://www.google.com"
  TIMEOUT = 3
end

class YahooGetter
  include HttpGettable
  URL = "http://www.yahoo.com"
  TIMEOUT =  5
end
</textarea>
<p>So which option is best?  That really depends on circumstances, aesthetic taste, and the teams ruby-fu as a whole.  Skipping the whole DSL option in favor of a less expressive but much simpler implementation is certainly a valid option.   Metaprogramming for metaprogramming&#8217;s sake is a bad idea that adds cost in complexity without compensation.  The Gettable example is intentionally trivial to clearly demonstrate the various options.  In the real world I probably would rely on constants for something so straightforward.  When trying to express multiple complex predicates I lean towards the DSL style&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class GoogleGetter
  include HttpGettable
  site "http://www.google.com", :timeout => 3, max_attempts => 5
  error_logging :level => :warn,  :frequency => :all_attempts
end
</textarea>
<p>ActiveRecord is another, more robust, example of where this style is clearly warranted.</p>
<p>Thanks to <a href="http://blog.jayfields.com/">Jay Fields</a>, <a href="http://jakescruggs.blogspot.com/">Jake Scruggs</a>, and <a href="http://elhumidor.blogspot.com/">John Hume</a> for their input on this topic.  I&#8217;d like to recommend a new shortcut by my friend and colleague Philippe Hanrigou.   <a href="http://www.informit.com/store/product.aspx?isbn=0321544684">Troubleshooting Ruby Processes: Leveraging System Tools when the Usual Ruby Tricks Stop Working</a> is an excellent resource for learning the tools and techniques for resolving those incredibly frustrating &#8220;why does my app just sit there doing nothing?&#8221; defects.  Hint: It probably isn&#8217;t a mongrel problem.  </p>
<p>As always thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2007/11/16/class-methods-part-iv-dsls/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Class Methods Part III - Structural Duplication</title>
		<link>http://www.klankboomklang.com/2007/11/12/class-methods-part-iii-structural-duplication/</link>
		<comments>http://www.klankboomklang.com/2007/11/12/class-methods-part-iii-structural-duplication/#comments</comments>
		<pubDate>Mon, 12 Nov 2007 22:53:13 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[patterns/idiom/style]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2007/11/12/class-methods-part-iii-structural-duplication/</guid>
		<description><![CDATA[This post in a series on class methods in Ruby focuses on removing structural duplication.
Whether it&#8217;s called OnceAndOnlyOnce or DRY good developers should agree that in the overwhelming majority cases duplication in software is a bad thing.  Duplication comes in many forms.  The simplest to identify (and remove) is actual text duplication.  [...]]]></description>
			<content:encoded><![CDATA[<p>This post in a series on class methods in Ruby focuses on removing structural duplication.</p>
<p>Whether it&#8217;s called <a href="http://c2.com/cgi/wiki?OnceAndOnlyOnce">OnceAndOnlyOnce</a> or <a href="http://www.artima.com/intv/dry.html">DRY</a> good developers should agree that in the overwhelming majority cases duplication in software is a bad thing.  Duplication comes in many forms.  The simplest to identify (and remove) is actual text duplication.  More difficult to find (and remove) is duplication of concerns.  Ruby class methods can help with the middle ground between these to extremes.  Take accessors.  Writing Ruby like this is grounds for the revocation of one&#8217;s programming license&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Ninja
  def weapon
    @weapon
  end

  def climbing_instrument
    @climbing_instrument
  end
end
</textarea>
<p>So what&#8217;s the responsibility of the weapon method?  How about the climbing_instrument method?  Both methods basically have the same concern, exposing access to instance data. Arguments about the wisdom of breaking encapsulation in this way aside, the Ninja class is breaking the DRY principle.  This concern, exposing instance data, is so common in fact that Ruby defines a class method on Module whose purpose is to dynamically define instance methods which expose instance data.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Ninja
  attr_reader :weapon, :climbing_instrument
end
</textarea>
<p>I call the sort of duplication eliminated by <b>attr_reader</b> structural duplication. When otherwise well factored code features heavy structural duplication, one sees methods where the words are different, but the whitespace is exactly the same.  Class methods, particularly when combined with convention and metaprogramming, are a clean way to remove this sort of duplication.  </p>
<p>Take for example an initialized value&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Ninja
  def weapon
    @weapon ||= :sword
  end	
end
</textarea>
<p>This sort of initialized value is prone to a classic bug.  The ninja can never be disarmed&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Ninja
  def weapon
    @weapon ||= :sword
  end

  def disarm
    @weapon = nil
  end
end

snake_eyes = Ninja.new
snake_eyes.disarm
snake_eyes.weapon
#=> :sword
</textarea>
<p>Useful as this may be to snake_eyes, it&#8217;s clearly not the intent of the disarm method.  The fix is straight forward&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Ninja
  def weapon
    self.class.class_eval do
      attr_reader :weapon
    end
    @weapon ||= :sword
  end
	
  def disarm
    @weapon = nil unless weapon.nil?
  end
end

snake_eyes = Ninja.new
snake_eyes.disarm
snake_eyes.weapon
#=> nil
</textarea>
<p>The weapon method redefines itself to be a simple attr_reader the first time it is accessed, it then initializes and returns @weapon. </p>
<p>Now assume that Ninja needs climbing instrument initialized in the same way&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Ninja
  def weapon
    self.class.class_eval do
      attr_reader :weapon
    end
    @weapon ||= :sword
  end

  def climbing_instrument
    self.class.class_eval do
      attr_reader :climbing_instrument
    end
    @climbing_instrument ||= :hook
  end
end
</textarea>
<p>Once again we can see a clear violation of DRY in the ninja class.  The logic for defining initialized attributes is duplicated across the weapon and climbing_instrument methods.  This logic can usefully be factored to a class method&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Ninja
  def self.attr_initialized(name, default_value)
    method_body = lambda do
      self.class.class_eval { attr_reader name }
      instance_variable_set "@#{name}", default_value
    end
    define_method name, method_body
  end
 
  attr_initialized :weapon, :sword 
  attr_initialized :climbing_instrument, :hook 	
end
</textarea>
<p>There are a couple points worth discussing here.  One is the implicit trade off made by this sort of class method.  A certain degree of readability has undeniably been lost in the pursuit of removing duplication.  Readability is one of the most important aspects of code quality, and changes that reduce readability should not be entered into lightly.  My inclination is to err towards readability for a very small amount of local duplication, and quickly switch towards slightly less readable better factored code when duplication surpasses minimal levels.</p>
<p>The other item worth exploring is where the attr_initialized method should live.  The interpreter must see the method definition before the class method can be used.  An alternative to cluttering up the class definition with the class method is to squirrel it away in a module.  This results in a very clean Ninja class&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Ninja
  extend InitializableAtrribute
  attr_initialized :weapon, :sword 
  attr_initialized :climbing_instrument, :hook 
end
</textarea>
<p>This touches on the concept of <a href="http://www.artima.com/intv/simplexity.html">simplexity</a>.  The Ninja class hasn&#8217;t actually been made simpler, the complexity has simply been hidden off somewhere out of site.  That said, once can certainly make the case that housing the attr_initialized method with the Ninja class is a severe violation of the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Single Responsibility Principle</a>.  In general, I recommend moving these class methods out to a separate module, and leaving the class definition free to express the domain intent.</p>
<p>Thanks to <a href="http://elhumidor.blogspot.com/">John Hume</a> and <a href="http://blog.jayfields.com/">Jay Fields</a> for their input and thoughts on structural duplication and class methods.  For more examples of of this class method usage, see <a href="http://www.jroller.com/abstractScope/entry/refactoring_ruby_code_via_metaprogramming">this post</a> by Farooq Ali.  As always, thanks for reading.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2007/11/12/class-methods-part-iii-structural-duplication/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Class Methods Part II - Annotations</title>
		<link>http://www.klankboomklang.com/2007/10/26/class-methods-part-ii-annotations/</link>
		<comments>http://www.klankboomklang.com/2007/10/26/class-methods-part-ii-annotations/#comments</comments>
		<pubDate>Fri, 26 Oct 2007 20:46:40 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[patterns/idiom/style]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2007/10/26/class-methods-part-ii-annotations/</guid>
		<description><![CDATA[Part II of this series on Ruby class methods is a look at annotations in Ruby.
The term annotation here refers to a Ruby technique that belongs to the same family as Java annotations, C# attributes, and C compiler directives.  These language features are of course not identical, and the class method derived Ruby version [...]]]></description>
			<content:encoded><![CDATA[<p>Part II of this series on Ruby class methods is a look at annotations in Ruby.</p>
<p>The term annotation here refers to a Ruby technique that belongs to the same family as Java annotations, C# attributes, and C compiler directives.  These language features are of course not identical, and the class method derived Ruby version is its own beast altogether.  Nevertheless, there are family resemblances.  Ruby class methods can be used to issue what might be called interpreter directives.  They can also be used to allow for an <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">Aspect Oriented Programming</a> (AOP) like abstraction of cross cutting concerns.</p>
<p>Take for an example a valuable but long running test that should only be run as part of an acceptance build.  In the (hopefully) rare case when the tests fails in the build environment, the developers responsible for the failed check-in can run the test locally to resolve the issue, but as a general rule developers don&#8217;t want to take the performance hit as part of their local test suite execution.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
require 'test/unit'
class CalculatorTest<Test::Unit::TestCase
  def test_some_complex_calculation
    assert_equal 2, Calculator.new(4).complex_calculation
  end
end  
</textarea>
<p>A simple interpreter directive might be implemented as&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class CalculatorTest<Test::Unit::TestCase
  if ENV["BUILD"] == "ACCEPTANCE"
    def test_some_complex_calculation
      assert_equal 2, Calculator.new(4).complex_calculation
    end
  end
end  
</textarea>
<p>On some systems, the incidence of this sort of test might climb to include a handful of &#8220;acceptance build only&#8221; tests.  At this point, it makes good sense to implement a custom interpreter directive.  There are three implementation options here&#8230;</p>
<p>Annotating Keyword&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class CalculatorTest<Test::Unit::TestCase
  extend TestDirectives

  acceptance_only
  def test_some_complex_calculation
    assert_equal 2, Calculator.new(4).complex_calculation
  end
end  
</textarea>
<p>Annotated Block&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class CalculatorTest<Test::Unit::TestCase
  extend TestDirectives

  acceptance_only do
    def test_some_complex_calculation
      assert_equal 2, Calculator.new(4).complex_calculation
    end
  end
end  
</textarea>
<p>Annotated Method Declaration&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class CalculatorTest<Test::Unit::TestCase
  extend TestDirectives

  acceptance_only :test_some_complex_calculation do
    assert_equal 2, Calculator.new(4).complex_calculation
  end
end  
</textarea>
<p>Let&#8217;s take a look at the implementation of an Annotating Keyword&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
module TestDirectives
  def acceptance_only
    @acceptance_build = ENV["BUILD"] == "ACCEPTANCE"
  end
  
  def method_added(method_name)
    remove_method(method_name) unless @acceptance_build
    @acceptance_build = false
  end
end
</textarea>
<p>CalculatorTest extends the TestDirective Module, so methods defined in TestDirective will be included as class methods in CalculatorTest.  When the interpreter encounters the <b>acceptance_only</b> keyword, it&#8217;s recognized as a call to the <b>acceptance_only</b> class method.  This method simply sets a class level instance variable indicating wether or not the current build is an acceptance build.  The real work is done by <b>method_added</b>, a hook method provided by the Module class.  When a class (or module) is defined in Ruby, the actual class or module is created as soon as the interpreter encounters the opening declaration (e.g. &#8220;class Foo&#8221;).  As the interpreter encounters method definitions, it adds methods to the method table of the class.  After a new method is added, the <b>method_added</b> class method is called with the method name passed as a parameter.  In the Annotating Keyword implementation, <b>method_added</b> checks if the new method is valid based on state set by calls to <b>acceptance_only</b>.  If the method is not valid in the current context, <b>method_added</b> removes the method from the class definition.  As a final step, state is reset back to &#8220;normal&#8221;.  Note that we could choose to not reset state, making <b>method_added</b> work very much like method accessibility keywords (public/protected/private).  </p>
<p>The implementation of an Annotated Block&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
module TestDirectives
  def acceptance_only &#038;block
    block.call if ENV["BUILD"] == "ACCEPTANCE"
  end
end
</textarea>
<p>The greatest strength of an Annotated Block is its exceedingly straight forward implementation.  The method declaration itself is a parameter to the <b>acceptance_only</b> class method.  <b>acceptance_only</b> then chooses wether of not to evaluate the method definition based on on a check of the environment variable.</p>
<p>Lastly, here is the implementation of Annotated Method Declaration&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">

module TestDirectives
  def acceptance_only(method_name, &#038;method_body)
    if ENV["BUILD"] == "ACCEPTANCE"
      define_method method_name, method_body
    end
  end
end
</textarea>
<p>Conceptually, Annotated Method Declaration is very similar to Annotated Block.  The difference is that instead of passing a method declaration to the annotating class method, the method body and method name are passed, and the annotating method takes care of the plumbing of actually defining the method.</p>
<p>The CalculatorTest example demonstrates an interpreter directive style use of annotations.  These same techniques are useful for more AOPish situations.  Take the canonical AOP example of logging.  Logging is a cross cutting concern.  That is to say, logging code can be found throughout an application, tangled up with the actual work of a method. Take an example of a requirement that certain method calls must logged&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Approval
  def decline(approver, comment)
    Logger.log("called decline with #{approver.inspect} #{coment.inspect}")
    #implementation
  end
end

class ApprovalFormatter
  def render_comment
    Logger.log("called render_comment}")
    #implementation
  end
end

</textarea>
<p>An Annotating Keyword implementation would allow for a better separation of concerns &#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">

class Approval
  extend Loggable
  logged
  def decline(approver, comment)
    #implementation
  end
end

class ApprovalFormatter
  extend Loggable
  logged
  def render_comment
    #implementation
  end
end

</textarea>
<p>The Loggable implementation demonstrates the primary weaknesses of Annotating Keyword, the complexity of it&#8217;s implementation&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
module Loggable
  def logged
    @logged = true
  end
  
  def method_added(method_name)
    logged_method = @logged
    @logged = false
    
    if logged_method	
      original_method = :"unlogged_#{method_name.to_s}"
      alias_method original_method, method_name 

      define_method(method_name) do |*args|
        arg_string = args.collect{ |arg| arg.inspect + " " } unless args.empty?
        log_message = "called #{method_name}"
        log_message << " with #{arg_string}" if arg_string
        Logger.log log_message
        self.send(original_method, *args)
      end
    end
  end
end
</textarea>
<p>Briefly, <b>method_added</b> aliases the method to be logged and redefines the accessor to return a method which logs relevant information before calling the original method.</p>
<p>So which style is appropriate where?  Annotating Keyword is certainly the most aesthetically pleasant to use, but it suffer from what Anders Hejlsberg calls <a href="http://www.artima.com/intv/simplexity.html">simplexity</a>.  The simple usage syntax hides a complex implementation.  Truly simple solutions are simple from top to bottom.  This complexity increases when multiple annotations come into play.  I&#8217;m inclined to use it for interpreter directive style annotations, or where I need to chain AOP style annotations.  </p>
<p>Annotated Block is the middle-ground option.  It avoids the use of the <b>method_added</b> hook, leaving it free for other uses (some of which may not be fully within the developers control).  Like Annotating Keyword, it&#8217;s chain-able, albeit only through some unfortunately indented code.  Simplicity is a double edged sword here.  Because an Annotating Block know so little about the methods it wraps, it is limited in how it can interact with those methods.  In particular, it does not have access to the arguments of the methods it wraps.  The only way it can supplement its weak position is to maintain some sort of state that interacts with <b>method_added</b>, adding the complexity of Annotating Keyword.  I prefer it for simple interpreter directive usages limited to including or excluding the definition of methods or execution of a code block.</p>
<p>Annotated Method Declaration is the only option that allows for code execution before the annotated method is defined.  This is a powerful property when coupled with its access to the parameters passed to the called method.  The major drawback of Annotated Method Declaration is that it cannot be chained.  I prefer to use it for AOP style annotations when an inability to add multiple annotations is acceptable.</p>
<p>Martin Fowler has an entry on <a href="http://martinfowler.com/bliki/RubyAnnotations.html">RubyAnnotations</a> on his bliki.  Be sure to read Jay Field&#8217;s recent write up on <a href="http://blog.jayfields.com/2007/10/ruby-defining-class-methods.html">class method declaration syntax</a>.  Thanks to <a href="http://www.pgrs.net/">Paul Gross</a> for pointing out that a post on annotations should be part of this series.</p>
<p>Thanks for reading.  As always your comments are appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2007/10/26/class-methods-part-ii-annotations/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Class Methods</title>
		<link>http://www.klankboomklang.com/2007/10/19/class-methods/</link>
		<comments>http://www.klankboomklang.com/2007/10/19/class-methods/#comments</comments>
		<pubDate>Fri, 19 Oct 2007 19:34:43 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[patterns/idiom/style]]></category>

		<category><![CDATA[Smalltalk]]></category>

		<category><![CDATA[Ruby Internals]]></category>

		<category><![CDATA[MRI]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2007/10/19/class-methods/</guid>
		<description><![CDATA[In my last post I wrote that providing a home for class methods was the raison d’être of the Metaclass.  So why go through all the trouble?  What are class methods good for?  Four uses worth exploring are Object Creation, Annotations, DSL&#8217;s, and removal of structural duplication
This post is about Ruby class [...]]]></description>
			<content:encoded><![CDATA[<p>In my last post I wrote that providing a home for class methods was the raison d’être of the Metaclass.  So why go through all the trouble?  What are class methods good for?  Four uses worth exploring are Object Creation, Annotations, DSL&#8217;s, and removal of structural duplication</p>
<p>This post is about Ruby class methods and not Ruby static methods.  There is a good reason for this.  There are no static methods in Ruby.  </p>
<p>In Java and C# &#8220;static method&#8221; and &#8220;class method&#8221; are synonyms, with &#8220;static&#8221; indicating that the methods are statically bound.  What does it mean for a method to be statically bound?  Consider a normal (non-static) instance method in Java.  Client code that consumes the method doesn&#8217;t know at compile time what method body will be associated with that usage.  It may be the instance method defined on the class, but it may just as well be an overridden version of the method defined in a subclass.  The method implementation must be &#8220;dynamically bound&#8221; to the invocation at runtime.  The compiler doesn&#8217;t have this issue with class methods, as in Java and C# class methods cannot be overridden.  Classes methods in Java and C# are not invoked on class instance, but are rather scoped to the class name.  As class method references can only resolve to one location, the compiler can &#8220;bind&#8221; the reference once at compile time &#8220;statically&#8221;.  Classes methods in Ruby are normal instance methods stored on the class&#8217;s Metaclass.  Even if Ruby were a compiled language, there would be no way to know at compile time which implementation of a method would eventually be responding to a message passed to a class object at runtime.</p>
<p>In any OO language, the most common use of class methods is for object creation.  Every Ruby programmer is familiar with the primary object creation class method, <b>new</b>.  Most programmers have also had cause to define alternative class methods for instantiating an object.  In <a href="http://java.sun.com/docs/books/effective/">&#8220;Effective Java&#8221;</a>, Joshua Bloch enumerates the advantages of this sort of factory method.  These advantages are wholly applicable to Ruby.</p>
<p><strong>Class methods have names</strong></p>
<p>Java requires all method signatures to be unique.  Suppose a Java programmer wants to allow the instantiation of an object in two different ways.  Both instantiation methods rely on a boolean and an int parameter. One way to solve this is to define two constructors&#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
public class SomeClass {

    public SomeClass(boolean parameter1, int parameter2) {
    }

    public SomeClass(int parameter1, boolean parameter2) {
    }
}
</textarea>
<p>This is valid Java, but it&#8217;s just a wee bit hackish to be overloading methods based on the order of their parameters.  Consumers of the class must infer the difference between the two constructors from the parameter names or a reading of the source.  In Ruby this problem is more acute, firstly because of duck typing, and secondly because Ruby has no facilities for method overloading.  For example, consider a Ruby class defined as&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class SomeClass
	def initialize(parameter1, parameter2)
	end
	
	def initialize(parameter1)
	end
end

SomeClass.new 1, 2

# => ArgumentError: wrong number of arguments (2 for 1)
</textarea>
<p>The second definition of initialize completely replaces the first definition.  Hacked up solutions to this type of problem aren&#8217;t relegated to the Java world.  In fact, duck typing allows for some horribly creative solutions in Ruby.  WARNING!  Don&#8217;t do this&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class SomeClass
	def initialize(parameter1, parameter2 = nil)
		unless paramter2.nil?
			#construct one way
		else
			#construct another way
		end
end
</textarea>
<p>or this&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class SomeClass
	def initialize(parameter1, parameter2)
		if paramter2.is_a? Integer
			#construct one way
		else
			#construct another way
		end
end
</textarea>
<p>or this&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class SomeClass
	def initialize(parameter1, parameter2, flag_for_how_to_construct)
		if flag_for_how_to_construct == :one_way
			#construct one way
		else
			#construct another way
		end
end
</textarea>
<p>Instead strongly prefer&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class SomeClass
	def self.one_way(parameter1, parameter2)
	end
	def self.another_way(parameter1)
	end
end
</textarea>
<p>For an in the wild example of a factory class method in Ruby, look no further than <a href="http://railsmanual.org/class/ActiveRecord::Base/create">ActiveRecord:Base.create<br />
</a><br />
<strong>Class methods don&#8217;t have to return a new object</strong></p>
<p>Sending the <b>new</b> message to a class will always cause the interpreter to create a new instance of the class.  This occurs in  <b>rb_class_new_instance</b>, the function bound to the <b>new</b> method of Object.</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">
//objcet.c:1585
VALUE
rb_class_new_instance(argc, argv, klass)
    int argc;
    VALUE *argv;
    VALUE klass;
{
    VALUE obj;

    obj = rb_obj_alloc(klass);
    rb_obj_call_init(obj, argc, argv);

    return obj;
}
</textarea>
<p><b>rb_class_new_instance</b> calls <b>rb_obj_alloc</b> to allocate a new object which is handed off to the initialize method by <b>rb_obj_call_init</b>.  As an interesting side note, notice that you can avoid having the initialize methods called by instantiating objects by calling SomeClass.allocate which returns the return value of <b>rb_obj_alloc</b>.</p>
<p>Sometimes a new object is not required, but rather a reference to an instance from a pool of existing objects.  Database connections are one common example.  Class methods are an excellent solution to the problem of how to provide access to the instance pool.  When the available pool of instances is limited to one object, this solution is an implementation of the <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton Pattern</a>.  &#8220;Singleton&#8221; here refers to the OO pattern documented in <a href="http://www.amazon.com/gp/product/customer-reviews/0201633612/ref=cm_cr_dp_all_top/105-7396063-2299621?ie=UTF8&#038;n=283155&#038;s=books#customerReviews">Design Patterns</a> by Gang-Of-Four, not the Ruby specific <a href="http://www.klankboomklang.com/2007/09/21/the-singleton-class/">Singleton class.</a></p>
<p>The Ruby Standard library includes a <a href="http://www.ruby-doc.org/stdlib/libdoc/singleton/rdoc/index.html">Singleton module</a> to take the work out of implementing this pattern.  The source includes extensive documentation on the implementation, so I won&#8217;t cover it here.  Suffice to say that the module adds a class method, <b>instance</b> to the including class.</p>
<p>Why have this module at all?  The Ruby language already provides a facility for creating objects with only one instance, classes.  Why not define a singleton as a class&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Foo
	def self.some_behavior
	end
end
</textarea>
<p>The interpreter guarantees that there will only ever be one instance of Foo&#8217;s Metaclass, the Foo class itself.  The existence of metaclasses means this question is applicable to Smalltalk as well.  In <a href="http://www.amazon.com/Design-Patterns-Smalltalk-Companion-Software/dp/product-description/0201184621">&#8220;The Design Patterns Smalltalk Companion&#8221;</a> by Sherman R. Alpert, Kyle Brown, and Bobby Wolf, two cases are made against implementing the Singleton Pattern with class methods.  The first is conceptual.  Classes should not be domain objects, their role is to allow for accessing and instantiating domain objects.  Defining domain logic directly in classes is a perversion of this intent.  The second argument is pragmatic.  If a requirement is introduced that requires the usage of multiple instances of the formally singleton object, the refactoring away from class method calls is more laborious then a traditional Singleton pattern implementation.  Although &#8216;The Design Patterns Smalltalk Companion&#8221; is a valuable read for Ruby programmers, in this case I don&#8217;t find the arguments particularly compelling .  Class methods probably are the simplest thing that could possible work when Singleton Pattern like functionality is needed.</p>
<p><strong>Class methods can return subclasses</strong></p>
<p>A classic usage of factory methods is to return a subclass of the class receiving the message based on an input parameter.  This is good encapsulation, as the consumer of the class does not need to know the details of which subclasses exist and which is appropriate for it&#8217;s needs.<br />
If one is fortunate enough to have useful conventions in place, a bit a metaprogramming can make this usage particularly powerful.  For example instead of &#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Pirate
	def self.by_nationality(nationality)
		case(nationality)
		when :french then FrenchPirate.new
		when :dutch then DutchPirate.new
		when :english then EnglishPirate.new
		end
	end
end
</textarea>
<p>one could implement by_nationality as&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Pirate
	def self.by_nationality(nationality)
		const_get("#{nationality.to_s.capitalize}Pirate").new
	end
end
</textarea>
<p>Not only is this implementation more succinct, it extensible to handle any Pirate nationality that follows the convention.  Keep in mind that classes aren&#8217;t the only place to store behavior.  Class methods might return a new instance of the class with modules mixed in&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Pirate
	def self.by_nationality(nationality)
		Pirate.new.extend const_get("ActsAs#{nationality.to_s.capitalize}")
	end
end
</textarea>
<p>Perhaps even more powerful, a class method might alter the Singleton class of an instance before retuning the object.  This is essentially subclassing on the fly.  Suppose Pirate&#8217;s have fighting behavior.  A class method could be created to return a pirate with a customized fighting style&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class SavateStyle
  def fight
    puts "lots of kicking"
  end
end

class Pirate
	def self.with_custom_fighting_style(style)
		pirate = Pirate.new
		pirate.singleton_class.send(:define_method, :fight, lambda { style.fight} )
    pirate
	end
end

Pirate.with_custom_fighting_style(SavateStyle.new).fight
# => lots of kicking
</textarea>
<p>Even further out on the metaprogramming ledge, the consumer could define the fighting style directly&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
class Pirate
	def self.with_custom_fighting_style(&#038;style)
		pirate = Pirate.new
		pirate.singleton_class.send(:define_method, :fight, &#038;style )
    pirate
	end
end

Pirate.with_custom_fighting_style do
  puts "lots of kicking"
end.fight

# => lots of kicking
</textarea>
<p>This last example, while interesting, doesn&#8217;t strike me as good style.  The consumer knowing both that it needs a Pirate to fight, and the implementation details of that fighting style seems like an awful lot for the consumer to know.</p>
<p>That covers using class methods for object creation.  I&#8217;ll be covering class methods usage for annotations, DSL&#8217;s, and removal of structural duplication in the next post(s).  As always thanks for reading.  I&#8217;m always happy to get your feedback, inquiries, corrections, and criticism.</p>
<p>Thanks to <a href="http://elhumidor.blogspot.com/">John Hume</a> for contributing his insight and expertise to this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2007/10/19/class-methods/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Objects, Classes, and JRuby Internals</title>
		<link>http://www.klankboomklang.com/2007/10/12/objects-classes-and-jruby-internals/</link>
		<comments>http://www.klankboomklang.com/2007/10/12/objects-classes-and-jruby-internals/#comments</comments>
		<pubDate>Fri, 12 Oct 2007 16:56:32 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[JRuby]]></category>

		<category><![CDATA[Ruby Internals]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2007/10/12/objects-classes-and-jruby-internals/</guid>
		<description><![CDATA[A walk through of the JRuby implementation wraps up this series on Ruby internals and Singleton classes.  
JRuby is a Java implementation of the Ruby interpreter.  That is to say, that one can write plain ol&#8217; Ruby files and hand them off to the JRuby interpreter for execution in the same way that [...]]]></description>
			<content:encoded><![CDATA[<p>A walk through of the JRuby implementation wraps up this series on Ruby internals and Singleton classes.  </p>
<p><a href="http://jruby.codehaus.org/">JRuby</a> is a Java implementation of the Ruby interpreter.  That is to say, that one can write plain ol&#8217; Ruby files and hand them off to the JRuby interpreter for execution in the same way that they can be executed via Matz&#8217;s Ruby interpreter (MRI).  From a Ruby object model perspective, JRuby isn&#8217;t doing anything all that different from MRI.  Specifically, a Ruby class Pirate, when interpreted by JRuby ,is NOT transformed into a Java Pirate class.  Rather, Pirate is represented by an instance of RubyClass in much the same way that MRI uses structs.  </p>
<p>JRuby itself is very important, and gaining a familiarity with it&#8217;s implementation is a good idea, independent of MRI.  That said, the parallels between the two interpreters are a boon for any developer in acquiring an advanced understanding of Ruby internals.  Inspection of JRuby source can give insight and perspective into how MRI works.  This is particularly useful to developers more comfortable in Java than C.  </p>
<p>In JRuby, Ruby objects are represented by instances of the RubyObject class&#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">

public class RubyObject implements Cloneable, IRubyObject {
//RubyObject.java:69
    protected RubyClass metaClass;
    protected Map instanceVariables;
    protected int flags; // zeroed by jvm
// ...

</textarea>
<p>The data members on RubyObject should look very <a href="http://www.klankboomklang.com/2007/09/21/the-singleton-class/">familiar</a>, they map one to one to RObject and RBasic in MRI.  There are a few interesting differences.  </p>
<p>The Ruby class of an object, stored as <b>klass</b> in MRI, is stored in a data member named <b>metaClass</b>.  This can be very confusing.  The value stored in <b>metaClass</b> is not necessarily a Metaclass or even a Singleton class in the Ruby sense.  It is simply the class of the object.  If the object is an instance of Class, than <b>metaClass</b> does indeed store a Metaclass, if it is a &#8220;normal&#8221; object, it stores either a &#8220;normal&#8221; Ruby class or a Singleton class, depending on whether or not instance specific behavior has been defined.  </p>
<p><b>flags</b> also work a bit differently in JRuby.  They are used to store  interesting state for an object (is it frozen? tainted?) as in MRI, but certain information that would be stored in <b>flags</b> in MRI is simply defined as methods in JRuby.  JRuby has the advantage of being able to subclass RubyObject, making it possible to represent this information differently by overriding the methods in subclasses.  For example&#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">

//RubyObject.java:313
public boolean isFrozen() {
    return (flags &#038; FROZEN_F) != 0;
}

//RubyObject.java:225
public boolean isSingleton() {
    return false;
}

//MetaClass.java:45
public boolean isSingleton() {
    return true;
}

</textarea>
<p>Like MRI, JRuby hides the actual class of an object by returning a <a href="http://www.klankboomklang.com/2007/09/28/real-class/">&#8220;real class&#8221;</a>.  The differences are just OO implementation details.  Rather than inspect the class itself, RubyObject asks it&#8217;s class for it&#8217;s &#8220;real class&#8221;&#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">

//RubyObject.java:1048
public RubyClass type() {
    return getMetaClass().getRealClass();
}

</textarea>
<p>If the the object&#8217;s <b>metaClass</b> is storing a Singleton class, represented by the JRuby MetaClass class, the class delegates to it&#8217;s super&#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">

//MetaClass.java:57
public RubyClass getRealClass() {
    return getSuperClass().getRealClass();
}

</textarea>
<p>Eventually the interpreter hits upon a &#8220;real class&#8221; represented by an instance of RubyClass, which returns itself&#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
//RubyClass.java:301
public RubyClass getRealClass() {
    return this;
}
</textarea>
<p>In both flavors of Ruby it&#8217;s convenient to say, &#8220;the Singleton class is created when instance specific behavior is added to an object&#8221;.  A more accurate statement is, &#8220;a Singleton class is created the first time an attempt is made to access the Singleton class of an object&#8221;.  In practice the two are often equivalent.  Once again JRuby&#8217;s behavior parallels MRI&#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">

public RubyClass getSingletonClass() {
    RubyClass klass;
    
    if (getMetaClass().isSingleton() &#038;&
        getMetaClass().getInstanceVariable("__attached__") == this) {
        klass = getMetaClass();            
    } else {
        klass = makeMetaClass(getMetaClass(), getMetaClass());
    }
    
    klass.setTaint(isTaint());
    klass.setFrozen(isFrozen());
    
    return klass;
}

</textarea>
<p>Here, the interpreter is checking the Java object stored in <b>metaClass</b> to see if it is a <a href="http://www.klankboomklang.com/2007/09/21/the-singleton-class/">Singleton</a> class associated with the object.  If so it returns the object stored there, otherwise it generates a new Singleton class by calling <b>makeMetaClass</b>.  As in MRI, <b>getSingletonClass</b> passes it&#8217;s current class in for use as the superclass of the new Singleton class.  The second parameter, is used for lexical scoping and does not play directly into the object model.  For clarity I&#8217;ve omitted some of the <b>makeMetaClass</b> source &#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">

public RubyClass makeMetaClass(RubyClass superClass, RubyModule parent) {
    RubyClass klass = new MetaClass(getRuntime(), 
                                    superClass, 
                                    getMetaClass().getAllocator(), 
                                    parent);
    setMetaClass(klass);
    return klass;
}

</textarea>
<p><b>makeMetaClass</b> parallels MRI exactly.  The JRuby interpreter creates a new class and assigns it to the <b>metaClass</b> replacing the current class of the object.</p>
<p>In JRuby, classes are created in the static <b>newClass</b> method.  As all classes inherit directly from another class or the Object class, the interpreter calls <b>newSubClass</b> on the &#8220;superclass to be&#8221; to actually generate the class&#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
//RubyClass:404
public RubyClass newSubClass(String name, 
                             ObjectAllocator allocator,
                             RubyModule parent, 
                             boolean invokeInherited, 
                             boolean warnOnRedefinition) {

    RubyClass newClass = new RubyClass(runtime, 
                                       classClass, 
                                       this, 
                                       allocator, 
                                       parent, 
                                       name);

    newClass.makeMetaClass(getMetaClass(), newClass);
//..

</textarea>
<p>Importantly, <b>makeMetaClass</b> is called immediately after the new class&#8217;s instantiation to create the <a href="http://www.klankboomklang.com/2007/10/05/the-metaclass/">Metaclass</a> of the new class.  As this method is being called from the new class&#8217;s super class, the first parameter passed to <b>makeMetaClass</b>, <b>getMetaClass</b> refers to the class of the new class&#8217;s super class.  In other words, the &#8220;The superclass of the Metaclass is the Metaclass of the superclass.&#8221;</p>
<p>I encourage you to crack open the JRuby source yourself and take a look around.  This post is accurate as of JRuby 1.1.  The JRuby folks have lots of big and exciting changes coming down the pipeline with regards to the internal representation of classes.  Thanks to <a href="http://ola-bini.blogspot.com/">Ola Bini</a> for being good enough to answer some of my JRuby questions and to the entire JRuby team for gifting us with their work.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2007/10/12/objects-classes-and-jruby-internals/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Metaclass</title>
		<link>http://www.klankboomklang.com/2007/10/05/the-metaclass/</link>
		<comments>http://www.klankboomklang.com/2007/10/05/the-metaclass/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 17:34:28 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[Ruby Internals]]></category>

		<category><![CDATA[MRI]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2007/10/05/the-metaclass/</guid>
		<description><![CDATA[If you search around the web a bit, you are sure to find the Ruby Metaclass defined as &#8220;The singleton class of a class.&#8221;  While this definition is accurate, it doesn&#8217;t contribute to an understanding of why Metaclasses exist.  It is also guilty of underplaying some notable difference between Metaclasses and &#8220;normal&#8221; Singleton [...]]]></description>
			<content:encoded><![CDATA[<p>If you search around the web a bit, you are sure to find the Ruby Metaclass defined as &#8220;The singleton class of a class.&#8221;  While this definition is accurate, it doesn&#8217;t contribute to an understanding of why Metaclasses exist.  It is also guilty of underplaying some notable difference between Metaclasses and &#8220;normal&#8221; Singleton class.</p>
<p>Before getting into Metaclass details it might be useful to clear up a bit of terminology.  Unfortunately, terminology around Ruby&#8217;s object model is not used consistently in existing literature, MRI source, JRuby source, or throughout the community.  A prime example of this is the <b>rb_make_metaclass</b> function in MRI, which is used to make both Metaclasses and Singleton classes.  That said, the following glossary is the one I stick with, and recommend to others.</p>
<ul>
<li><strong>Singleton Class:</strong>  <a href="http://www.klankboomklang.com/2007/09/21/the-singleton-class/">Hidden Ruby class for storing instance specific behavior</a></li>
<li><strong>Metaclass:</strong> The Singleton class of a Class object.  Differs in some ways from a &#8220;normal&#8221; Singleton class.  Occasionally inappropriately used as a synonym for Singleton class.</li>
<li><strong>Eigenclass:</strong>  Alternative name for a Singleton Class.  Eigenclass (roughly &#8220;own class&#8221; in German) has the advantage of not having a <a href="http://en.wikipedia.org/wiki/Singleton_pattern">well known OO design pattern</a> named after it. It&#8217;s preferred by some of the <a href="http://www.eigenclass.org/">Ruby elite</a>, but has never really caught on with the community at large.</li>
<li><strong>Virtual Class:</strong> Alternative name for a Singleton Class.  Best avoided.  In the current MRI implementation, there is nothing particularly virtual about these classes and virtual is already a very loaded term in the OO community.</li>
</ul>
<p>Take a look a page 382 of <a href="http://www.pragmaticprogrammer.com/titles/ruby/index.html">PickAxe, Second edition</a> for some additional background on Metaclass terminology.</p>
<p>With diction out of the way, focus can return to the question, &#8220;What is it that Ruby wants that forces Metaclasses to exist?&#8221;  This can be answered in a number of ways, but one simple answer is &#8220;Ruby wants class methods.&#8221;  </p>
<p>Take this simple factory method&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">

class Pirate
	def self.create_by_country(country)
		# FrenchPirate, DutchPirate, etc
	end
end

</textarea>
<p>Understanding what happens when the create_by_country message is sent to Pirate	is not difficult if one bares in mind Ruby&#8217;s &#8220;everything is an object&#8221; ethos.  If Ruby is going to treat the Pirate class as an object, then the class should have a <b>klass</b> pointer and <b>flags</b> collection. </p>
<p>Looking at the definition of the <b>RClass</b> struct confirms that the Pirate class is doing it&#8217;s part&#8230;</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">

//ruby.h:326
struct RClass {
    struct RBasic basic;
    struct st_table *iv_tbl;
    struct st_table *m_tbl;
    VALUE super;
};

</textarea>
<p><b>RClass</b> references an </b>RBasic</b> struct which includes a <b>klass</b> pointer and <b>flags</b> collection.  Note that with the inclusion of an <b>iv_tbl</b>, the data members of <b>RClass</b> are a superset of <b>RObject&#8217;s</b> members.  Put another way, anything represented by a <b>RClass</b> struct fulfills Ruby requirements for &#8220;objectness&#8221;.  Once it is understood that the Pirate class is itself an object, understanding how the create_by_country message is handled becomes a matter of understanding <a href="http://www.klankboomklang.com/2007/09/14/method-dispatch/">method dispatch</a>.  When the Pirate class receives the create_by_country message, the interpreter follows Pirate&#8217;s <b>klass</b> pointer and inspects the <b>m_tbl</b> it finds there for a method named create_by_country.</p>
<p>Pirate is an instance of Class, and so at first glance one would think that the class returned by Pirate&#8217;s <b>klass</b> pointer should be the Class class.  The problem is Ruby can&#8217;t store create_by_country in Class&#8217; <b>m_tbl</b>, or all instance of Class, that is to say every class in the Ruby process, would have access to the method.  This problem should sound <a href="http://www.klankboomklang.com/2007/09/21/the-singleton-class/">familiar</a>.  create_by_country is actually instance specific behavior for the Pirate instance of Class. Since instance specific behavior is stored in Singleton classes, Ruby does what it knows, and creates a Singleton class for Pirate to store the create_by_country method body.  This special Singleton class, found by following the <b>klass</b> pointer of a class instance is called a Metaclass.</p>
<p>If that were all there is to Metaclasses, The Pirate class object model would look something like this&#8230;</p>
<p><center><img src="/images/pirate_class_simple_and_wrong.jpg"></center></p>
<p>It turns out that Ruby needs a bit more from classes.  Since classes are objects, Ruby wants them to play nicely in an Object Oriented playground.  All the other children in that playground know a game called <a href="http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming">polymorphism</a>, and so classes must too.  Assume that the Pirate class inherits from the Human class.  Polymorphism (and <a href="http://www.objectmentor.com/resources/articles/lsp.pdf">Liskov&#8217;s Substitution Principle</a>) say that any instance of Human can be replaced with an instance of Pirate.  That is to say a Pirate <b>is a</b> Human, and so we can expect it to do all the things a human does; eat, sleep, etc&#8230;</p>
<p>Ruby wants this principle to be extended to classes.  So if Pirate extends Human, then the Pirate class <b>is a</b> Human class, and any class methods exposed by Human should be available as class methods on Pirate.  &#8220;Normal&#8221; Singleton class creation doesn&#8217;t support Ruby&#8217;s desire on this.  Remember that for a non-class Ruby object, the Singleton class replaces the &#8220;real&#8221; class, which stays in the dispatch loop via the <b>super</b> pointer on the Singleton class.  If Metaclasses worked the same way, then the Human/Pirate object model would look something like this&#8230;</p>
<p><center><img src="/images/pirate_class_with_human_wrong.jpg"></center></p>
<p>A message sent to Pirate would never be dispatched to &#8216;Human&#8217;s <b>m_tbl</b>.  If Ruby wants classes to be polymorphic, the interpreter needs a way to move from the Metaclass of Pirate to the Metaclass of Human when dispatching class methods.  The sanest way the interpreter can do this, is to use &#8216;Pirate&#8217;s <b>super</b> pointer, and so that is what it does. </p>
<p>The creation of &#8220;normal&#8221; Singleton classes and the creation of Metaclasses is handled by two different lines of code.  A quick look at this code makes this all a bit more concrete&#8230;</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">

//class.c:875 in rb_singleton_class
klass = rb_make_metaclass(obj, RBASIC(obj)->klass);

//object.c:1527 in rb_class_initialize
RCLASS(klass)->super = super;
rb_make_metaclass(klass, RBASIC(super)->klass);

</textarea>
<p>The second parameter to <b>rb_make_metaclass</b> will be used as the <b>super</b> pointer for the Singleton class that will be created.  Note that when making a &#8220;normal&#8221; Singleton class, this parameter is the current class of the object under manipulation.  When the object is itself a class however, as in <b>rb_class_initialize</b>, the second parameter is the class found by following the <b>klass</b> pointer of the object&#8217;s <b>super</b> pointer.  This is a bit convoluted, in fact it leads to one of the most confusing sentences in Ruby, &#8220;The superclass of the Metaclass is the Metaclass of the superclass&#8221;.  A corrected diagram clears this up&#8230;</p>
<p><center><img src="/images/pirate_class_with_human.jpg"></center></p>
<p>With the object model arranged this way, the interpreter is free to use standard method dispatch to resolve messages sent to a class object.  Note an additional bit of weirdness in this diagram.  An instances of Human <b>is a</b>n instance of Object.  This means that the Human class has an <b>is a</b> relationship with the Object class and should have access to any class methods that might be stored on Object.   Standard Metaclass rules apply, the super pointer of the Metaclass of Human must point to the Metaclass of Human&#8217;s superclass, Object.</p>
<p>Creating this parallel inheritance hierarchy as new classes are defined is one thing, but it would be painful in the extreme to have to re-situate the <b>super</b> pointers of all subclasses the first time a class defined a class method.  Ruby avoids this pain in an interesting way.  Notice that the code example above, to create the Metaclass, was extracted from a function named <b>rb_class_initialize</b>.  As this implies, Metaclasses are created at the time of a classes initialization.  This is markedly different form &#8220;normal&#8221; Singleton classes which are instantiated &#8220;on demand&#8221; at the time of first access.</p>
<p>As always, I hope you find this useful.  If you are looking for more in depth coverage of the Metaclass be sure to check out the translated fourth chapter of <a href="http://www.hawthorne-press.com/WebPage_RHG.html"> The Ruby Hackers Guide</a>.  Comments, questions, and corrections are always appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2007/10/05/the-metaclass/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Real Class</title>
		<link>http://www.klankboomklang.com/2007/09/28/real-class/</link>
		<comments>http://www.klankboomklang.com/2007/09/28/real-class/#comments</comments>
		<pubDate>Fri, 28 Sep 2007 16:23:56 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[Smalltalk]]></category>

		<category><![CDATA[JRuby]]></category>

		<category><![CDATA[Ruby Internals]]></category>

		<category><![CDATA[MRI]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2007/09/28/real-class/</guid>
		<description><![CDATA[It interesting that Ruby, a language which allows you to so easily bypass encapsulation, encapsulates it&#8217;s own internals to the point of occasional prevarication.  Heres an example&#8230;

pirate = Object.new
def pirate.speak
	"Yarrrr"
end
pirate.class == Object.new.class
#=> true

In my last post I explained how the code above forces the interpreter to assign a newly created Singleton class to the [...]]]></description>
			<content:encoded><![CDATA[<p>It interesting that Ruby, a language which allows you to so easily bypass encapsulation, encapsulates it&#8217;s own internals to the point of occasional prevarication.  Heres an example&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
pirate = Object.new
def pirate.speak
	"Yarrrr"
end
pirate.class == Object.new.class
#=> true
</textarea>
<p>In my <a href="http://www.klankboomklang.com/2007/09/21/the-singleton-class/">last post</a> I explained how the code above forces the interpreter to assign a newly created Singleton class to the pirate object.  If that&#8217;s true, how is it possible that the class of a new Object is equal to the class of pirate?  It&#8217;s not possible, the Ruby interpreter is more or less lying.</p>
<p><b>rb_obj_class</b> in <b>object.c</b> is the the underlying C function that is accessed when the class message is sent to an object.  It&#8217;s definition&#8230;</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">
//object.c 156
VALUE
rb_obj_class(obj)
    VALUE obj;
{
    return rb_class_real(CLASS_OF(obj));
}
</textarea>
<p><b>rb_obj_class</b> calls the function <b>rb_class_real</b> on the return value of <b>CLASS_OF</b> the recipient of the class message.  <b>CLASS_OF</b> is a macro which expands into a call to <b>rb_class_of</b>.  I&#8217;ll skip the details of <b>rb_class_of</b>, in most cases it simply returns the <b>RBASIC klass</b> pointer, i.e. the struct representing the class of the object.</p>
<p>In the case of the pirate object, the &#8216;pirate Singleton class is passed to <b>rb_class_real</b>.  From there on out,  Ruby&#8217;s deception is straight forward&#8230;</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">
//object.c 116
VALUE
rb_class_real(cl)
    VALUE cl;
{
    while (FL_TEST(cl, FL_SINGLETON) || TYPE(cl) == T_ICLASS) {
	cl = RCLASS(cl)->super;
    }
    return cl;
}
</textarea>
<p><b>rb_class_real</b> uses a couple of macros to inspect the <b>RBASIC flags</b> of the class, checking if it is a Singleton class or Include class (<b>T_ICLASS</b>).  Include classes are special proxy classes that are created when a module is included.  I&#8217;ll cover them in greater detail in a later post.  The relevant thing here, is that Include classes are similar to Singleton classes in that the interpreter considers them an implementation detail which should not be exposed to language consumers (this means you).  If the class under inspection is not a Singleton or Include class, it is returned, otherwise the super pointer is followed and the checks are run again.  In this way, the interpreter loops through the class hierarchy of the object looking for a &#8220;normal&#8221; class.  For the pirate object, the super pointer for &#8216;pirate points to Object, and so Object is returned from <b>rb_real_class</b>.</p>
<p>Knowing what the interpreter does, doesn&#8217;t answer the question of why it does it.  Wouldn&#8217;t it be simpler (and more honest) to return the Singleton &#8216;pirate class when the class message is sent to pirate?  There are a couple reasons why Ruby works this way.  </p>
<p>Firstly, Matz views Singleton and Include classes as implementation details. Yes, Ruby let&#8217;s you add instance specific behavior and mixin modules, but how it does it is not really your business.  It&#8217;s not an accident that the language does not have a built-in singleton_class method.  While this is a reasonable view to have, it&#8217;s worth bearing in mind that the existence of <b>class << self</b> as a language construct, more or less releases the Singleton class from Pandora&#8217;s box of encapsulation.</p>
<p>There is another more subtle reason for the class method to work this way.  As far as I know this was first identified by Kent Beck.  Kent wrote a two part article in 1993 for &#8220;Smalltalk Report&#8221;, <a href="http://www.macqueen.us/apache2-default/www.macqueen.us/smalltalkReport/ST/91_95/SMAL0206.PDF">&#8220;Instance-Specific Behavior: How and Why&#8221;</a> and <a href="http://www.macqueen.us/apache2-default/www.macqueen.us/smalltalkReport/ST/91_95/SMAL0207.PDF">Instance-Specific Behavior: Digitalk Implementation and the Deeper Meaning of it All&#8221;</a>.  In these articles Kent detailed a pattern for adding instance specific behavior in Smalltalk which is nearly identical to what would later be known as the Singleton class in Ruby.   Kent&#8217;s pattern notabley does not include anything like <b>rb_real_class</b>. </p>
<p>Two years later, in 1995, Kent published an article for &#8220;Smalltalk Report&#8221; regrettably entitled, <a href="http://www.macqueen.us/apache2-default/www.macqueen.us/smalltalkReport/ST/91_95/SMAL0406.PDF">&#8220;What, What happened to Garbage Collection?&#8221;</a>, in which he pointed out a problem with the Instance-Specific Behavior pattern.  Assume that equality for a Point class is defined as below&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
def ==(other_point)
	other_point.class == Point &#038;& other_point.x == self.x &#038;& other_point.y == self.y ? true : false
end
</textarea>
<p>Without the existence of something like <b>rb_real_class</b>, two points which share an x and y coordinate will not be equal if one of them has defined a Singleton class.  </p>
<p>This is an interesting problem.  From a purist&#8217;s view, it&#8217;s fair to say that they are no longer in fact equal as they are no longer interchangeable.  One of the Points is now capable of differing or additional behavior.  From a pragmatist&#8217;s view however, having equality semantics change because of added instance specific behavior could create nightmarish situations as various frameworks and libraries relying on the original semantic begin to breakdown in subtle and difficult to deduce ways.</p>
<p>Kent&#8217;s pragmatic solution is to introduce a <b>realClass</b> method.  Singleton classes (actually an instance of Behavior in Smalltalk), return the <b>realClass</b> of their super.  Classes return themselves. <b>Object>>class</b> is redefined to send a <b>realClass</b> message to it&#8217;s associated class.  </p>
<p>This is exactly the same behavior as <b>rb_real_class</b>, albeit implemented in an OO fashion.  In fact looking at JRuby, an OO implementation of the Ruby interpreter, we find exactly the same pattern.  In JRuby, Singleton classes are represented by instances of MetaClass&#8230;</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
public class RubyClass extends RubyModule {
  public RubyClass getRealClass() {
    return this;
  }
}

public class MetaClass extends RubyClass {
  public RubyClass getRealClass() {
    return getSuperClass().getRealClass();
  }
}
</textarea>
<p>Thanks to my colleague and friend <a href="http://elhumidor.blogspot.com/">John Hume</a> for his input on this post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2007/09/28/real-class/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Singleton Class</title>
		<link>http://www.klankboomklang.com/2007/09/21/the-singleton-class/</link>
		<comments>http://www.klankboomklang.com/2007/09/21/the-singleton-class/#comments</comments>
		<pubDate>Fri, 21 Sep 2007 17:47:37 +0000</pubDate>
		<dc:creator>pfarley</dc:creator>
		
		<category><![CDATA[Ruby Internals]]></category>

		<category><![CDATA[MRI]]></category>

		<guid isPermaLink="false">http://www.klankboomklang.com/2007/09/21/the-singleton-class/</guid>
		<description><![CDATA[There&#8217;s no shortage of literature on singleton classes, but it&#8217;s rare to see an explanation that covers implementation.  This is unfortunate, as the implementation is not complex and familiarity with it leads to a more nuanced understanding.
A good way to look at singleton classes is to ask the question, &#8220;What is it that Ruby [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s no shortage of literature on singleton classes, but it&#8217;s rare to see an explanation that covers implementation.  This is unfortunate, as the implementation is not complex and familiarity with it leads to a more nuanced understanding.</p>
<p>A good way to look at singleton classes is to ask the question, &#8220;What is it that Ruby wants which forces the existence of a singleton class?&#8221;  The answer is, &#8220;Ruby wants instance specific behavior&#8221;.  </p>
<p>An example&#8230;</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">

pirate = Object.new

def pirate.speak
  puts "Arrrrrrrr"
end

pirate.speak
#=> Arrrrrrr

Object.new.speak
# NoMethodError: 
# undefined method `speak' for #<Object:0x214904>

</textarea>
<p>Here the speak method has been added to an instance of Object called pirate.  The pirate object will respond to the speak method, but other objects know nothing of it.  So how does Ruby make this happen?  One option would be to store the behavior right along with the object (a la prototyped languages like JavaScript), but a quick look at the C struct that represents an object show that this is not the option Matz took.</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">
//Ruby.h:321  
struct RObject {
    struct RBasic basic;
    struct st_table *iv_tbl;
};
</textarea>
<p>An object has only two data members, a hash to store instance variables called <b>*iv_tbl</b> and a reference to another struct of type RBasic called <b>basic</b>.  RBasic is the fundamental struct in Ruby.  It holds the data all Ruby &#8220;things&#8221; need for the interpreter to work with them.    It&#8217;s definition is&#8230;</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">
//Ruby.h:316  
struct RBasic {
    unsigned long flags;
    VALUE klass;
};
</textarea>
<p><b>flags</b> are used to store metadata about an object.  An example would be a bit to indicate an object is frozen or tainted.  The only other data member is the class associated with the object, stored in <b>klass</b>.  </p>
<p>Note that there is nowhere in RBasic or RObject to tuck away a method body.  That leaves the class associated with the <b>klass</b> pointer as the only place left to store the instance specific behavior.  This makes a lot of sense.  The entire purpose of classes in Ruby is to serve as repositories for behavior. Here Ruby finds itself in a tough spot.  If the interpreter follows the <b>klass</b> pointer of the pirate object, it will find the Object class.  Any method&#8217;s added there would be accessible to any instance of Object.  </p>
<p>Faced with a dilemma, Ruby does something sneaky.  When an attempt is made to define instance specific behavior, Ruby generates an instance of a brand new class and changes the object&#8217;s class pointer to point to this new class.  It then adds the homeless instance specific behavior to the new class. Now, when the message for instance specific behavior is sent to the object, standard <a href="http://www.klankboomklang.com/2007/09/14/method-dispatch/">method dispatch</a> kicks in, and the method body is found.  This special, created on the fly, instance specific class, is called the Singleton class.  </p>
<p>There is one more trick involved.  It wouldn&#8217;t do for the object to lose all the behavior it picked up from it&#8217;s class of instantiation, so Ruby sets the super pointer of the Singleton class to point to the original class of the object.  Once again standard <a href="http://www.klankboomklang.com/2007/09/14/method-dispatch/">method dispatch</a> works just fine when searching for methods. </p>
<p>Below are a couple of diagrams showing the pirate object&#8217;s underlying structure before and after the speak method is defined.  The prefixed apostrophe on <b>&#8216;pirate</b> can be read as &#8220;The Singleton class of pirate.&#8221;</p>
<p><center></p>
<div style="padding-top: 10px; padding-bottom: 30px;">
<img src="/images/pirate_without_singleton_class.jpg" alt="pirate with singleton class" /><br />
<strong>Before Definition of Instance Specific Behavior</strong>
</div>
<div style="padding-bottom: 10px;">
<img src="/images/pirate_with_singleton_class.jpg" alt="pirate with singleton class" /><br />
<strong>After Definition of Instance Specific Behavior</strong>
</div>
<p></center></p>
<p>All of this class inheritance chain manipulation occurs in the rb_make_metaclass() function.  I&#8217;ve omitted some code for clarity.</p>
<textarea name="code" class="c:nocontrols:nogutter" cols="60" rows="10">
//Class.c:150 
VALUE
rb_make_metaclass(obj, super)
    VALUE obj, super;
{
    VALUE klass = rb_class_boot(super);
    RBASIC(obj)->klass = klass;
    return klass;
}
</textarea>
<p>If you find all this interesting and useful, I strongly encourage you to take a look at the <a href="http://www.hawthorne-press.com/WebPage_RHG.html">Ruby Hackers Guide.</a>  It&#8217;s a guide to Ruby internals written by <a href="http://i.loveruby.net/">Minero Aoki</a>.  Unfortunately, translation is an on going project that has possibly stalled out.  Still, the chapters which have been translated are a wealth of Ruby internals information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.klankboomklang.com/2007/09/21/the-singleton-class/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
