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

<channel>
	<title>Code Commit &#187; Ruby</title>
	<atom:link href="http://www.codecommit.com/blog/category/ruby/feed" rel="self" type="application/rss+xml" />
	<link>http://www.codecommit.com/blog</link>
	<description>(permanently in beta)</description>
	<lastBuildDate>Fri, 27 Jan 2012 14:35:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<atom:link rel='hub' href='http://www.codecommit.com/blog/?pushpress=hub'/>
		<item>
		<title>Monads Are Not Metaphors</title>
		<link>http://www.codecommit.com/blog/ruby/monads-are-not-metaphors</link>
		<comments>http://www.codecommit.com/blog/ruby/monads-are-not-metaphors#comments</comments>
		<pubDate>Mon, 27 Dec 2010 08:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/?p=329</guid>
		<description><![CDATA[This article is also available in Japanese.
I am about to break a promise.  Almost three years ago, I promised myself that I would never write an article about monads.  There are too many such articles already; so many, in fact, that people are often confused by the sheer proliferation.  Everyone seems to [...]]]></description>
			<content:encoded><![CDATA[<p><em>This article is also <a href="http://eed3si9n.com/ja/monads-are-not-metaphors">available in Japanese</a>.</em></p>
<p>I am about to break a promise.  Almost three years ago, I promised myself that I would <em>never</em> write an article about monads.  There are too many such articles already; so many, in fact, that people are often confused by the sheer proliferation.  Everyone seems to have a different take on the subject, meaning that those attempting to learn the concept for the first time are stuck trying to reason out the commonalities between burritos, space suits, elephants and desert Bedouins.</p>
<p>I&#8217;m not going to add to this menagerie of confusing analogies.  The fact is that none of these parallels are entirely accurate.  None of them convey the whole picture, and some of them are blatantly misleading in important respects.  You will <em>never</em> come to understand monads by pondering Mexican food and the Final Frontier.  The only way to understand monads is to see them for what they are: a mathematical construct.</p>
<h3>Math (or not)</h3>
<p>Here&#8217;s the thing about monads which is hard to grasp: monads are a pattern, not a specific type.  Monads are a <em>shape</em>, they are an abstract interface (not in the Java sense) more than they are a concrete data structure.  As a result, <em>any</em> example-driven tutorial is doomed to incompleteness and failure.  The only way to really understand is to take a step back and look at what monads mean in the <em>abstract</em> rather than the concrete.  Take a look at the following Ruby snippet:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">def</span> foo<span class="br0">&#40;</span>bar<span class="br0">&#41;</span>
  puts bar
  bar.<span class="me1">size</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Just as a quick Ruby refresher, we can rewrite this code in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">def</span> foo<span class="br0">&#40;</span>bar<span class="br0">&#41;</span>
  puts bar; bar.<span class="me1">size</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Ruby has this neat convention (which is shared by most modern languages) which causes the final expression in a method to be turned into the implicit return statement.  Thus, the <code>foo</code> method will take a parameter, print it to standard out and then return its <code>size</code>.  Fairly simple, right?</p>
<p>Here&#8217;s the puzzler: what is the semicolon (<code>;</code>) doing?  It&#8217;s tempting to say that it&#8217;s just a separator, but theoretically speaking, there&#8217;s something much more interesting going on here.  Let&#8217;s switch to Scala and add some Christmas trimmings:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>bar: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span> = <span class="br0">&#123;</span>
  <span class="br0">&#40;</span><span class="br0">&#123;</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>bar<span class="br0">&#41;</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
  <span class="br0">&#40;</span><span class="br0">&#123;</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; bar.<span class="me1">length</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Just in case you&#8217;re unfamiliar with Scala, I&#8217;d like to make it clear that we are <em>not</em> required to enclose every statement inside its own lambda (anonymous function).  I&#8217;m just doing that to make a point.</p>
<p>This function does exactly the same thing as the Ruby version.  Well, the parameter is a bit more constrained since we require a <code>String</code> rather than accepting anything defines <code>size</code>, but moving past that&hellip;  The major difference from what we had previously is that each statement is wrapped inside its own anonymous function, which we immediately call.  We can again play the same semicolon trick that we used in Ruby.  However, because the statements are actually functions, we can go a step further:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>bar: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span> = <span class="br0">&#123;</span>
  <span class="br0">&#40;</span><span class="br0">&#123;</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>bar<span class="br0">&#41;</span> <span class="br0">&#125;</span> andThen <span class="br0">&#123;</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; bar.<span class="me1">length</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>(note: the <code>andThen</code> method isn&#8217;t defined for functions of 0-arity, but we&#8217;re going to pretend that it is and that it works the same as it does for functions of one argument.  If it makes you feel better, you can pretend that these are both one-argument functions taking <code>Unit</code> as a parameter, the theoretical implications are the same, it just requires more syntax)</p>
<p>Notice that we haven&#8217;t actually used the semicolon (although we could have).  Instead, we&#8217;re <em>combining</em> two functions together and invoking them at the very end.  The semantics we&#8217;re using to do the combination are such that the first function will be evaluated, then its result (<code>()</code>) discarded and the second function evaluated with its result returned.  For those following along at home, we could easily define <code>andThen</code> in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> funcSyntax<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>f1: <span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; A<span class="br0">&#41;</span> = <span style="color: #140dcc;">new</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> andThen<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#40;</span>f2: <span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; B<span class="br0">&#41;</span> = <span style="color: #2e7c0f;">f1</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; <span style="color: #2e7c0f;">f2</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>In a way, we have defined a method which literally encapsulates the effect of the semicolon &#8220;operator&#8221;, allowing us to apply it directly to functions, rather than dealing with it indirectly at the level of statements.  That&#8217;s kind of a cool thought, but the important point is that we are first executing the first function, discarding its result and then executing the second function, returning its result.</p>
<p>It should be clear that we could extend this to any number of functions.  For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>bar: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span> = <span class="br0">&#123;</span>
  <span class="br0">&#40;</span><span class="br0">&#123;</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Executing foo&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span> andThen
   <span class="br0">&#123;</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>bar<span class="br0">&#41;</span> <span class="br0">&#125;</span> andThen
   <span class="br0">&#123;</span> <span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; bar.<span class="me1">length</span> <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Still with me?  Congratulations, you&#8217;ve seen your first monad.</p>
<h3><a href="http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html">You Could Have Invented Monads! (and maybe you already have)</a></h3>
<p>This certainly isn&#8217;t a monad in the traditional sense, but if we worked at it, we could show that the monadic axioms do hold.  The significant point here is what this monad is doing: combining one thing together with another in sequence.  In fact, this is what <em>all</em> monads do, deep down.  You start out with Thing One, and you have a function which will (given One) will give you Thing Two.  Monads let you combine Thing One and your function together, producing a final resultant Thing.  Let&#8217;s look at some more code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">case</span> <span style="color: #140dcc;">class</span> Thing<span style="color: #7f0055;"><span class="br0">&#91;</span>+A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>value: A<span class="br0">&#41;</span></pre></td></tr></table></div>

<p>This is about the simplest container imaginable (in fact, it is <em>precisely</em> the simplest container imaginable, but that&#8217;s not relevant now).  We can wrap up values inside of <code>Thing</code>, but that&#8217;s about it:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> a = <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span><span class="br0">&#41;</span>
<span style="color: #140dcc;">val</span> b = <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #cb0710;">2</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p>Now, let&#8217;s switch into design mode for a moment.  Imagine that we find ourselves writing a lot of code which looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>i: <span style="color: #800080;">Int</span><span class="br0">&#41;</span> = <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span>i + <span style="color: #cb0710;">1</span><span class="br0">&#41;</span>
&nbsp;
<span style="color: #140dcc;">val</span> a = <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span><span class="br0">&#41;</span>
<span style="color: #140dcc;">val</span> b = <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>a.<span class="me1">value</span><span class="br0">&#41;</span>        <span style="color: #999999;">// =&gt; Thing(2)</span></pre></td></tr></table></div>

<p>We&#8217;re starting with a <code>Thing</code>, and then we&#8217;re using the value inside of that <code>Thing</code> to call a function which gives us a new <code>Thing</code>.  If you think about it, this is actually a very common pattern.  We have a value, and then we use that value to compute a new value.  Mathematically, this is pretty much the same as the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>i: <span style="color: #800080;">Int</span><span class="br0">&#41;</span> = i + <span style="color: #cb0710;">1</span>
&nbsp;
<span style="color: #140dcc;">val</span> a = <span style="color: #cb0710;">1</span>
<span style="color: #140dcc;">val</span> b = <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>              <span style="color: #999999;">// =&gt; 2</span></pre></td></tr></table></div>

<p>The only difference between these is that the first version wraps everything in <code>Thing</code>, while the second version is using &#8220;bare&#8221; values.</p>
<p>Now, let&#8217;s extend our imagine just a bit and assume that we have a good reason for wrapping everything inside of <code>Thing</code>.  There could of course be any number of reasons for this, but basically it boils down to the notion that <code>Thing</code> might have some extra logic which does interesting things with its value.  Here&#8217;s the question: can we come up with a nicer way of going from <code>a</code> to <code>b</code>?  Basically, we want to encapsulate this pattern as a more general tool.</p>
<p>What we want is a function which pulls the value out of <code>Thing</code> and then calls another function with that value, returning the result of that function call (which will be a new <code>Thing</code>).  Since we&#8217;re good object-oriented programmers, we will define this as a method on class <code>Thing</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">case</span> <span style="color: #140dcc;">class</span> Thing<span style="color: #7f0055;"><span class="br0">&#91;</span>+A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>value: A<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> bind<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#40;</span>f: A =&gt; Thing<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#41;</span> = <span style="color: #2e7c0f;">f</span><span class="br0">&#40;</span>value<span class="br0">&#41;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>So if we have a <code>Thing</code>, we can pull its value out and use it to compute a new <code>Thing</code>, all in one convenient step:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>i: <span style="color: #800080;">Int</span><span class="br0">&#41;</span> = <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span>i + <span style="color: #cb0710;">1</span><span class="br0">&#41;</span>
&nbsp;
<span style="color: #140dcc;">val</span> a = <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span><span class="br0">&#41;</span>
<span style="color: #140dcc;">val</span> b = a bind foo          <span style="color: #999999;">// =&gt; Thing(2)</span></pre></td></tr></table></div>

<p>Notice that this is a lot cleaner that our original version, while still performing exactly the same function.  <code>Thing</code> is a monad.</p>
<h4>The Monad Pattern</h4>
<p>Any time you start with something which you pull apart and use to compute a new something of that same type, you have a monad.  It&#8217;s really as simple as that.  If it sounds like I&#8217;m describing almost all of your code, then good, that means you&#8217;re starting to catch on.  Monads are everywhere.  And by &#8220;everywhere&#8221;, I do mean <em>everywhere</em>.</p>
<p>To understand why this is, let&#8217;s look at what it is that makes <code>Thing</code> a monad:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> a = <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p>The first thing is that I can wrap up a value inside of a new <code>Thing</code>.  Object-oriented developers might call this a &#8220;constructor&#8221;.  Monads call it &#8220;the <code>unit</code> function&#8221;.  Haskell calls it &#8220;<code>return</code>&#8221; (maybe we shouldn&#8217;t try to figure out that one just yet).  Whatever you call it though, it comes to the same thing.  We have a function of type <code>A =&gt; Thing</code>; a function which takes some value and wraps it up inside a new <code>Thing</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala">a bind <span class="br0">&#123;</span> i =&gt; <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span>i + <span style="color: #cb0710;">1</span><span class="br0">&#41;</span> <span class="br0">&#125;</span></pre></td></tr></table></div>

<p>We also have this fancy <code>bind</code> function, which digs inside our <code>Thing</code> and allows a function which we supply to use that value to create a new <code>Thing</code>.  Scala calls this function &#8220;<code>flatMap</code>&#8220;.  Haskell calls it &#8220;<code>&gt;&gt;=</code>&#8220;.  Again, the name doesn&#8217;t matter.  What&#8217;s interesting here is the fact that <code>bind</code> is how you combine two things together in sequence.  <strong>We start with one thing and use its value to compute a new thing.</strong></p>
<p>It&#8217;s as simple as that!  If you&#8217;re like me, then you&#8217;re likely asking the following question: if it&#8217;s so simple, what&#8217;s all the fuss about?  Why not just call this the &#8220;using one thing to compute another&#8221; pattern?  Well, for one thing, that&#8217;s far too verbose.  For another, monads were first defined by mathematicians, and mathematicians <em>love</em> to name things.  Mathematics is all about finding patterns, and it&#8217;s hard to find patterns if you can&#8217;t affix labels once you&#8217;ve found them.</p>
<h3>More Examples</h3>
<p>I said that monads are everywhere (they are!), but we&#8217;ve only looked at two examples so far.  Let&#8217;s see a few more.</p>
<h4>Option</h4>
<p>This might be the most famous monad of all, quite possibly because it&#8217;s one of the easiest to understand and by far the easiest to motivate.  Consider the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">firstName</span><span class="br0">&#40;</span>id: <span style="color: #800080;">Int</span><span class="br0">&#41;</span>: <span style="color: #857d1f;">String</span> = ...    <span style="color: #999999;">// fetch from database</span>
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">lastName</span><span class="br0">&#40;</span>id: <span style="color: #800080;">Int</span><span class="br0">&#41;</span>: <span style="color: #857d1f;">String</span> = ...
&nbsp;
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">fullName</span><span class="br0">&#40;</span>id: <span style="color: #800080;">Int</span><span class="br0">&#41;</span>: <span style="color: #857d1f;">String</span> = <span class="br0">&#123;</span>
  <span style="color: #140dcc;">val</span> fname = <span style="color: #2e7c0f;">firstName</span><span class="br0">&#40;</span>id<span class="br0">&#41;</span>
  <span style="color: #140dcc;">if</span> <span class="br0">&#40;</span>fname != <span style="color: #a40d14;">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span style="color: #140dcc;">val</span> lname = <span style="color: #2e7c0f;">lastName</span><span class="br0">&#40;</span>id<span class="br0">&#41;</span>
    <span style="color: #140dcc;">if</span> <span class="br0">&#40;</span>lname != <span style="color: #a40d14;">null</span><span class="br0">&#41;</span>
      fname + <span style="color: #cb0710;">&quot; &quot;</span> + lname
    <span style="color: #140dcc;">else</span>
      <span style="color: #a40d14;">null</span>
  <span class="br0">&#125;</span> <span style="color: #140dcc;">else</span> <span class="br0">&#123;</span>
    <span style="color: #a40d14;">null</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Here again, we have a fairly common pattern.  We have two functions (<code>firstName</code> and <code>lastName</code>) which are responsible for producing some data which may or may not be available.  If the data is available, then it is returned.  Otherwise, the result of these functions will be <code>null</code>.  We then use these functions to do something interest (in this case, compute a full name).  Unfortunately, the fact that <code>firstName</code> and <code>lastName</code> may or may not produce a useful value needs to be handled explicitly with a set of nested <code>if</code>s.</p>
<p>At first blush, it seems this is the best that we can do.  However, if you look very closely, you can find the monad pattern buried in this code.  It&#8217;s a little more complicated than last time, but it&#8217;s still there.  Let&#8217;s try wrapping everything in <code>Thing</code> to make it clear:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">firstName</span><span class="br0">&#40;</span>id: <span style="color: #800080;">Int</span><span class="br0">&#41;</span>: Thing<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span> = ...    <span style="color: #999999;">// fetch from database</span>
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">lastName</span><span class="br0">&#40;</span>id: <span style="color: #800080;">Int</span><span class="br0">&#41;</span>: Thing<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span> = ...
&nbsp;
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">fullName</span><span class="br0">&#40;</span>id: <span style="color: #800080;">Int</span><span class="br0">&#41;</span>: Thing<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span> = <span class="br0">&#123;</span>
  <span style="color: #2e7c0f;">firstName</span><span class="br0">&#40;</span>id<span class="br0">&#41;</span> bind <span class="br0">&#123;</span> fname =&gt;
    <span style="color: #140dcc;">if</span> <span class="br0">&#40;</span>fname != <span style="color: #a40d14;">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span style="color: #2e7c0f;">lastName</span><span class="br0">&#40;</span>id<span class="br0">&#41;</span> bind <span class="br0">&#123;</span> lname =&gt;
        <span style="color: #140dcc;">if</span> <span class="br0">&#40;</span>lname != <span style="color: #a40d14;">null</span><span class="br0">&#41;</span>
          <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span>fname + <span style="color: #cb0710;">&quot; &quot;</span> + lname<span class="br0">&#41;</span>
        <span style="color: #140dcc;">else</span>
          <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #a40d14;">null</span><span class="br0">&#41;</span>
      <span class="br0">&#125;</span>
    <span class="br0">&#125;</span> <span style="color: #140dcc;">else</span> <span class="br0">&#123;</span>
      <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #a40d14;">null</span><span class="br0">&#41;</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>See it now?  As I said, monads are everywhere.  Here&#8217;s the really useful bit though: every time we <code>bind</code>, the very first thing we do <em>inside</em> the function is test the value to see if it is <code>null</code>, why not move that logic into <code>bind</code>?  Of course, we can&#8217;t really do that without changing <code>Thing</code> into something different, so we will define a new monad called <code>Option</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">sealed</span> <span style="color: #140dcc;">trait</span> Option<span style="color: #7f0055;"><span class="br0">&#91;</span>+A<span class="br0">&#93;</span></span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> bind<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#40;</span>f: A =&gt; Option<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#41;</span>: Option<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">case</span> <span style="color: #140dcc;">class</span> Some<span style="color: #7f0055;"><span class="br0">&#91;</span>+A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>value: A<span class="br0">&#41;</span> <span style="color: #140dcc;">extends</span> Option<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> bind<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#40;</span>f: A =&gt; Option<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#41;</span> = <span style="color: #2e7c0f;">f</span><span class="br0">&#40;</span>value<span class="br0">&#41;</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">case</span> <span style="color: #140dcc;">object</span> <span style="color: #663f31;">None</span> <span style="color: #140dcc;">extends</span> Option<span style="color: #7f0055;"><span class="br0">&#91;</span>Nothing<span class="br0">&#93;</span></span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> bind<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#40;</span>f: Nothing =&gt; Option<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#41;</span> = <span style="color: #663f31;">None</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>If you block out everything except <code>Some</code>, this looks a <em>lot</em> like our old friend, <code>Thing</code>.  The main difference is that <code>Option</code> has two different instantiations: <code>Some</code>, which contains a value, and <code>None</code>, which doesn&#8217;t contain a value.  Think of <code>None</code> as being just an easier way of writing <code>Thing(null)</code>.</p>
<p>What&#8217;s interesting is that <code>Some</code> and <code>None</code> need to have two different definitions of <code>bind</code>.  The definition of <code>bind</code> in <code>Some</code> looks a lot like the definition in <code>Thing</code>, which makes sense as <code>Some</code> and <code>Thing</code> are almost identical.  However, <code>None</code> defines <code>bind</code> to always return <code>None</code>, ignoring the specified function.  How does this help us?  Well, let&#8217;s return to our <code>fullName</code> example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">firstName</span><span class="br0">&#40;</span>id: <span style="color: #800080;">Int</span><span class="br0">&#41;</span>: Option<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span> = ...    <span style="color: #999999;">// fetch from database</span>
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">lastName</span><span class="br0">&#40;</span>id: <span style="color: #800080;">Int</span><span class="br0">&#41;</span>: Option<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span> = ...
&nbsp;
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">fullName</span><span class="br0">&#40;</span>id: <span style="color: #800080;">Int</span><span class="br0">&#41;</span>: Option<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span> = <span class="br0">&#123;</span>
  <span style="color: #2e7c0f;">firstName</span><span class="br0">&#40;</span>id<span class="br0">&#41;</span> bind <span class="br0">&#123;</span> fname =&gt;
    <span style="color: #2e7c0f;">lastName</span><span class="br0">&#40;</span>id<span class="br0">&#41;</span> bind <span class="br0">&#123;</span> lname =&gt;
      <span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span>fname + <span style="color: #cb0710;">&quot; &quot;</span> + lname<span class="br0">&#41;</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>All of those nasty <code>if</code> statements have disappeared.  This works because <code>firstName</code> and <code>lastName</code> now return <code>None</code> when they fail to fetch the database record, rather than <code>Thing(null)</code>.  Of course, if we try to <code>bind</code> on <code>None</code>, the result will always be <code>None</code>.  Thus, the <code>fullName</code> function returns the combination of <code>firstName</code> and <code>lastName</code> inside of a <code>Some</code> instance only if neither <code>firstName</code> nor <code>lastName</code> return <code>None</code>.  If either one returns <code>None</code>, then the result of the whole thing will be <code>None</code>.</p>
<p>For those keeping score at home, we have &#8220;accidentally&#8221; stumbled upon Groovy&#8217;s <a href="http://groovy.codehaus.org/Null+Object+Pattern">safe-dereference operator</a> (<code>?.</code>), <a href="https://github.com/raganwald/andand">Raganwald&#8217;s <code>andand</code></a> for Ruby and many, many more.  See?  Monads are everywhere.</p>
<h3>IO</h3>
<p>Anyone trying to understand monads will inevitably run into Haskell&#8217;s <code>IO</code> monad, and the results are almost always the same: bewilderment, confusion, anger, and ultimately Perl.  The fact is that <code>IO</code> is a rather odd monad.  It is fundamentally in the same category as <code>Thing</code> and <code>Option</code>, but it solves a very different problem.</p>
<p>Here&#8217;s the deal: Haskell doesn&#8217;t allow side-effects.  At all.  Functions take parameters and return values; you can&#8217;t just &#8220;change&#8221; something outside the function.  As an example of what this means, let&#8217;s return to our earlier Ruby code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">def</span> foo<span class="br0">&#40;</span>bar<span class="br0">&#41;</span>
  puts bar
  bar.<span class="me1">size</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>This function takes a value, calls its <code>size</code> method and returns the result.  However, it also <em>changes</em> the standard output stream.  This is pretty much the same as if we had a global array hanging around which we just mutated in-place:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #857d1f;">STDOUT</span> = <span class="br0">&#91;</span><span class="br0">&#93;</span>
&nbsp;
<span style="color: #140dcc;">def</span> foo<span class="br0">&#40;</span>bar<span class="br0">&#41;</span>
  <span style="color: #857d1f;">STDOUT</span> += <span class="br0">&#91;</span>bar<span class="br0">&#93;</span>
  bar.<span class="me1">size</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Haskell doesn&#8217;t have variables of any sort (imagine if Scala <em>didn&#8217;t</em> have <code>var</code>, or if every variable in Java were <code>final</code>).  Since we don&#8217;t have any variables, we can&#8217;t change anything in-place.  Since we can&#8217;t change anything in-place, there&#8217;s no way we can define a <code>puts</code> function.  At least, not the <code>puts</code> we&#8217;re used to.</p>
<p>Let&#8217;s switch back to Scala.  Our goal here is to define a <code>println</code> function which <em>doesn&#8217;t</em> rely on any sort of mutable state (ignoring for the moment the fact that the standard output stream is always going to be mutable, since there&#8217;s no way we can &#8220;change&#8221; the user&#8217;s screen by cloning the physical display).  One thing we could do is wrap up our standard output stream as a <code>Vector</code> which we carry along with our functions:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>bar: <span style="color: #857d1f;">String</span>, stdout: Vector<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span><span class="br0">&#41;</span> = <span class="br0">&#123;</span>
  <span style="color: #140dcc;">val</span> stdout2 = <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>bar, stdout<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>bar.<span class="me1">length</span>, stdout2<span class="br0">&#41;</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>str: <span style="color: #857d1f;">String</span>, stdout: Vector<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span><span class="br0">&#41;</span> = stdout + str</pre></td></tr></table></div>

<p>Theoretically, we could write all of our <code>println</code>-enabled functions in this way, passing in the current <code>stdout</code> and receiving the new state as a result.  At the end of the day, the entire program would produce a result in addition to the final state of <code>stdout</code>, which could be printed to the screen by the language runtime.</p>
<p>This works (at least for <code>println</code>), but it&#8217;s ridiculously ugly.  I would hate it if I had to write code like this, and early Haskell adopters felt exactly the same way.  Unfortunately, things only get worse from there.  Our trick with <code>Vector[String]</code> may work for the standard output stream, but what about standard in?  At first blush, it <em>seems</em> as if the <code>readLine</code> function wouldn&#8217;t be as bad as <code>println</code>, after all, we aren&#8217;t changing anything!  Unfortunately, something is clearly being changed at some level, otherwise repeated calls to <code>readLine</code> would yield the same result (clearly not the case).</p>
<p>Graphics updates, networking, the list goes on.  It turns out that any <em>useful</em> program is going to need to have side-effects, otherwise there&#8217;s no way for all of that usefulness to get <em>outside</em> the program where we can see it.  So, Haskell&#8217;s designers (more specifically, Phillip Wadler) needed to come up with a way to solve this problem not only for standard out, but for <em>all</em> side-effects.</p>
<p>The solution is actually quite simple.  To solve the standard out problem with <code>println</code>, we just passed a <code>Vector[String]</code> around, &#8220;modifying&#8221; it by returning the new state along with our regular return value.  Here&#8217;s the inspiration: what if we did that for the <em>entire universe?</em>  Instead of passing around just a plain <code>Vector[String]</code>, we pass around <code>Universe</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>bar: <span style="color: #857d1f;">String</span>, everything: Universe<span class="br0">&#41;</span> = <span class="br0">&#123;</span>
  <span style="color: #140dcc;">val</span> everything2 = <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>bar, everything<span class="br0">&#41;</span>
  <span class="br0">&#40;</span>bar.<span class="me1">length</span>, everything2<span class="br0">&#41;</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>str: <span style="color: #857d1f;">String</span>, everything: Universe<span class="br0">&#41;</span> = everything.<span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>str<span class="br0">&#41;</span></pre></td></tr></table></div>

<p>As long as the language runtime is able to somehow give us an instance of <code>Universe</code> which behaves the way we would expect, then this code will work just fine.  Obviously, the runtime can&#8217;t <em>really</em> package up the entire cosmos and allow us to get new versions of it, but it can cheat a bit and <em>pretend</em> to give us the entire universe.  The language runtime is allowed to implement the <code>println</code> function on the <code>Universe</code> object in whatever way it deems best (hopefully by actually appending to standard out).  Thus, we let the runtime perform whatever magic is necessary while we remain blissfully ignorant of any and all side-effects.</p>
<p>This solves our problems alright, but it&#8217;s horrible in almost every other respect.  We have this manual threading of the <code>Universe</code>, which is both verbose and painful.  Even worse, this sort of thing is very error prone (i.e. what happens if we &#8220;modify&#8221; the universe and then go back and change an older version of it?  do we get two, parallel universes?).  The heart of our problem now is that we are using the old version of the universe to compute a new version of the universe, and we&#8217;re doing that manually.  We&#8217;re taking something (in this case, the universe) and using its value to compute a new something (the new version of the universe).  Sound familiar?</p>
<p>Phillip Wadler&#8217;s inspiration was to take advantage of the monad pattern to solve this problem.  The result is the <code>IO</code> monad:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">foo</span><span class="br0">&#40;</span>bar: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span>: IO<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #800080;">Int</span><span class="br0">&#93;</span></span> = <span class="br0">&#123;</span>
  <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>bar<span class="br0">&#41;</span> bind <span class="br0">&#123;</span> _ =&gt; <span style="color: #2e7c0f;">IO</span><span class="br0">&#40;</span>bar.<span class="me1">length</span><span class="br0">&#41;</span> <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>str: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span>: IO<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #800080;">Unit</span><span class="br0">&#93;</span></span> = <span class="br0">&#123;</span>
  <span style="color: #999999;">// TODO insert magic incantations</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Of course, we can&#8217;t <em>actually</em> implement <code>println</code>, since this fake language isn&#8217;t allowed to involve itself in side-effects.  However, the language runtime could provide a native (read: cheat code) implementation of <code>println</code> which performs its side-effects and then conjures a new version of <code>IO</code> (i.e. the modified universe).</p>
<p>The only catch with this design is that we can never get anything <em>out</em> of <code>IO</code>.  Once you start down the dark path, forever will it dominate your destiny.  The reason for this is one of language purity.  Haskell wants to disallow side-effects, but if it were to allow us to pull values out of <code>IO</code>, then we could very easily bypass its safe-guards:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">readLine</span><span class="br0">&#40;</span><span class="br0">&#41;</span>: IO<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span> = <span class="br0">&#123;</span>
  <span style="color: #999999;">// TODO insert magic incantations</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">fakeReadLine</span><span class="br0">&#40;</span>str: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span>: <span style="color: #857d1f;">String</span> = <span class="br0">&#123;</span>
  <span style="color: #140dcc;">val</span> back: IO<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span> = <span style="color: #2e7c0f;">readLine</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
  back.<span class="me1">get</span>      <span style="color: #999999;">// whew!  doesn't work</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>As you can see, if we could pull values <em>out</em> of <code>IO</code>, then the whole exercise would become a waste of time, since it would be trivially easy to hide side-effects within wrapper functions.</p>
<p>Of course, none of this is particularly relevant to Scala or Ruby, much less Java!  Scala doesn&#8217;t restrict side-effects.  It allows you to call <code>println</code> whenever you feel like it, and it provides a way of declaring mutable variables (<code>var</code>).  In a sense, Scala is hiding a hypothetical <code>IO</code> monad.  It&#8217;s as if <em>all</em> Scala code is implicitly inside of <code>IO</code>, and thus implicitly threading the state of the universe from one moment to the next.  In light of this fact, why should we care at all about how <code>IO</code> works?  Mostly because it&#8217;s a monad which is very different from <code>Thing</code> and <code>Option</code>.  I briefly considered using <code>State</code>, but that&#8217;s too much ancillary complexity when we&#8217;re trying to focus on the core idea of using the value from one thing to compute another thing.</p>
<h3>Now What?</h3>
<p>So, we&#8217;ve identified the monad pattern.  We can spot it in code and employ it in an impressive range of situations, but why are we bothering with all the ceremony?  If we&#8217;re already using monads all over the place without realizing it (e.g. semicolon), then why do we have to bother futzing with all of this nomenclature?  Simply put: why do we care that <code>Option</code> is a monad so long as it just &#8220;does the right thing&#8221;?</p>
<p>Well, the first answer to that lies in the nature of mathematics, and by extension, computer programming.  As I said, before, mathematics is all about identifying patterns (well, you also play with those patterns to generate larger, more intricate patterns, but that&#8217;s really just a means to an end).  Once you identify a pattern, you name it.  That&#8217;s just what mathematicians do.  Imagine if Newton hadn&#8217;t named the &#8220;derivative&#8221;, and he simply insisted on calling it &#8220;an expression for the line tangent to a given line relative to a particular value of <em>x</em>, where <em>x</em> is some variable in the expression for the given line.&#8221;  For one thing, calculus textbooks everywhere would be about 50 times longer.  For another, we probably never would have been able to see &#8220;the derivative&#8221; as an abstract entity.  Partial derivation would have never been devised.  Integrals, differential equations, infinite series, and almost all of physics might never have happened.  None of these consequences have anything to do with the <em>name</em>, that&#8217;s just a label.  Rather, it was the fact that Newton was able to see (and represent) the derivative in the abstract, as a mathematical <em>shape</em> to be manipulated and applied in novel ways.</p>
<p>If you understand the <code>Option</code> monad, then you can use the <code>Option</code> monad.  You can see places in your code where it can be applied, and you will reap enormous benefits as a result.  However, if you understand monads as an abstraction, then you will not only understand <code>Option</code>, but also <code>IO</code>, <code>State</code>, <code>Parser</code>, <code>STM</code>, the list goes on.  Or at least, you will understand the fundamental properties of these constructs (the rest is details).  You will begin to see places where you are doing monadic things even when those things don&#8217;t fit exactly into the restricted mold of <code>Option</code> or <code>State</code>.  This is where the true utility can be found.</p>
<p>Besides the (vast) improvements in your thought process, there&#8217;s also a more immediate practical upshot.  It&#8217;s possible to define functions which work on monads in the generic sense, rather than specializing in one or the other.  Just as Swing programming would be impossible if you had to rewrite every function for each specific instance of <code>Component</code>, there are many things which would be impossible (or at least, very very impractical) if you had to rewrite them for each specific monad.  One such function is <code>sequence</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">trait</span> Monad<span style="color: #7f0055;"><span class="br0">&#91;</span>+M<span class="br0">&#91;</span>_<span class="br0">&#93;</span></span><span class="br0">&#93;</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> unit<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>a: A<span class="br0">&#41;</span>: M<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span>
  <span style="color: #140dcc;">def</span> bind<span style="color: #7f0055;"><span class="br0">&#91;</span>A, B<span class="br0">&#93;</span></span><span class="br0">&#40;</span>m: M<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#41;</span><span class="br0">&#40;</span>f: A =&gt; M<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#41;</span>: M<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">implicit</span> <span style="color: #140dcc;">object</span> ThingMonad <span style="color: #140dcc;">extends</span> Monad<span style="color: #7f0055;"><span class="br0">&#91;</span>Thing<span class="br0">&#93;</span></span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> unit<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>a: A<span class="br0">&#41;</span> = <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>
  <span style="color: #140dcc;">def</span> bind<span style="color: #7f0055;"><span class="br0">&#91;</span>A, B<span class="br0">&#93;</span></span><span class="br0">&#40;</span>thing: Thing<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#41;</span><span class="br0">&#40;</span>f: A =&gt; Thing<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#41;</span> = thing bind f
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">implicit</span> <span style="color: #140dcc;">object</span> OptionMonad <span style="color: #140dcc;">extends</span> Monad<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">Option</span><span class="br0">&#93;</span></span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> unit<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>a: A<span class="br0">&#41;</span> = <span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>
  <span style="color: #140dcc;">def</span> bind<span style="color: #7f0055;"><span class="br0">&#91;</span>A, B<span class="br0">&#93;</span></span><span class="br0">&#40;</span>opt: Option<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#41;</span><span class="br0">&#40;</span>f: A =&gt; Option<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#41;</span> = opt bind f
<span class="br0">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #140dcc;">def</span> sequence<span style="color: #7f0055;"><span class="br0">&#91;</span>M<span class="br0">&#91;</span>_<span class="br0">&#93;</span></span>, A<span class="br0">&#93;</span><span class="br0">&#40;</span>ms: List<span style="color: #7f0055;"><span class="br0">&#91;</span>M<span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span style="color: #140dcc;">implicit</span> tc: Monad<span style="color: #7f0055;"><span class="br0">&#91;</span>M<span class="br0">&#93;</span></span><span class="br0">&#41;</span> = <span class="br0">&#123;</span>
  ms.<span style="color: #2e7c0f;">foldRight</span><span class="br0">&#40;</span>tc.<span style="color: #2e7c0f;">unit</span><span class="br0">&#40;</span>List<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#40;</span>m, acc<span class="br0">&#41;</span> =&gt;
    tc.<span style="color: #2e7c0f;">bind</span><span class="br0">&#40;</span>m<span class="br0">&#41;</span> <span class="br0">&#123;</span> a =&gt; tc.<span style="color: #2e7c0f;">bind</span><span class="br0">&#40;</span>acc<span class="br0">&#41;</span> <span class="br0">&#123;</span> tail =&gt; tc.<span style="color: #2e7c0f;">unit</span><span class="br0">&#40;</span>a :: tail<span class="br0">&#41;</span> <span class="br0">&#125;</span> <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>There are a lot of ways to pretty this up, but I wanted to be as explicit as possible for the sake of illustration.  The general function of <code>sequence</code> is to take a <code>List</code> of monad instances and return a monad instance of a <code>List</code> of those elements.  For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> nums = <span style="color: #2e7c0f;">List</span><span class="br0">&#40;</span><span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span><span class="br0">&#41;</span>, <span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span><span style="color: #cb0710;">2</span><span class="br0">&#41;</span>, <span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span><span style="color: #cb0710;">3</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
<span style="color: #140dcc;">val</span> nums2 = <span style="color: #2e7c0f;">sequence</span><span class="br0">&#40;</span>nums<span class="br0">&#41;</span>           <span style="color: #999999;">// Some(List(1, 2, 3))</span></pre></td></tr></table></div>

<p>The magic is that this function works on <em>any</em> monad:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> nums = <span style="color: #2e7c0f;">List</span><span class="br0">&#40;</span><span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span><span class="br0">&#41;</span>, <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #cb0710;">2</span><span class="br0">&#41;</span>, <span style="color: #2e7c0f;">Thing</span><span class="br0">&#40;</span><span style="color: #cb0710;">3</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
<span style="color: #140dcc;">val</span> nums2 = <span style="color: #2e7c0f;">sequence</span><span class="br0">&#40;</span>nums<span class="br0">&#41;</span>           <span style="color: #999999;">// Thing(List(1, 2, 3))</span></pre></td></tr></table></div>

<p>In this case, <code>Monad</code> (the trait) is an example of a <em>typeclass</em>.  Basically, we&#8217;re saying that there is this general idea of a monad, and any monad will define two functions: <code>unit</code> and <code>bind</code>.  In this way, define functions which operate on monads without knowing which <em>specific</em> monad we&#8217;re manipulating.  Think of it like the adapter pattern on steroids (sprinkled with a goodly portion of Scala&#8217;s implicit magic).</p>
<p>This is reason number two for understanding the <em>general</em> concept of a monad, rather than just the specific: you suddenly become able to define tons of nifty utility functions.  For more examples of this, you need look no further than <a href="http://members.chello.nl/hjgtuyl/tourdemonad.html">Haskell&#8217;s standard library</a>.</p>
<h3>Those Pesky Axioms</h3>
<p>Congratulations!  You just made it through an entire monad tutorial without ever having to worry about the monadic axioms or what they mean.  Now that you&#8217;ve got the concept down (hopefully), you can graduate to the axioms themselves.</p>
<p>As it turns out, the monadic axioms are really quite intuitive.  We&#8217;ve actually been assuming them all along without ever really saying so.  The axioms define how the <code>unit</code> (constructor) and <code>bind</code> (composition) functions are supposed to behave under certain situations.  Think of them a bit like the laws governing integer addition (commutativity, associativity, etc).  They don&#8217;t tell you <em>everything</em> about monads (actually, they don&#8217;t tell you much at all from an intuitive standpoint), but they do give you the basic, mathematical underpinnings.</p>
<p>Excited yet?  No, I didn&#8217;t think so.  Well, here they are anyway, defined in terms of the <code>Monad</code> typeclass we used earlier:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> axioms<span style="color: #7f0055;"><span class="br0">&#91;</span>M<span class="br0">&#91;</span>_<span class="br0">&#93;</span></span><span class="br0">&#93;</span><span class="br0">&#40;</span><span style="color: #140dcc;">implicit</span> tc: Monad<span style="color: #7f0055;"><span class="br0">&#91;</span>M<span class="br0">&#93;</span></span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span style="color: #999999;">// identity 1</span>
  <span style="color: #140dcc;">def</span> identity1<span style="color: #7f0055;"><span class="br0">&#91;</span>A, B<span class="br0">&#93;</span></span><span class="br0">&#40;</span>a: A, f: A =&gt; M<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span style="color: #140dcc;">val</span> ma: M<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span> = tc.<span style="color: #2e7c0f;">unit</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>
    <span style="color: #2e7c0f;">assert</span><span class="br0">&#40;</span>tc.<span style="color: #2e7c0f;">bind</span><span class="br0">&#40;</span>ma<span class="br0">&#41;</span><span class="br0">&#40;</span>f<span class="br0">&#41;</span> == <span style="color: #2e7c0f;">f</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span><span class="br0">&#41;</span>
  <span class="br0">&#125;</span>
&nbsp;
  forAll <span class="br0">&#123;</span> <span class="br0">&#40;</span>x, f<span class="br0">&#41;</span> =&gt; <span style="color: #2e7c0f;">identity1</span><span class="br0">&#40;</span>a, f<span class="br0">&#41;</span> <span class="br0">&#125;</span>        <span style="color: #999999;">// sort-of ScalaCheck</span>
&nbsp;
  <span style="color: #999999;">// identity 2</span>
  <span style="color: #140dcc;">def</span> identity2<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>ma: M<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span style="color: #2e7c0f;">assert</span><span class="br0">&#40;</span>tc.<span style="color: #2e7c0f;">bind</span><span class="br0">&#40;</span>ma<span class="br0">&#41;</span><span class="br0">&#40;</span>tc.<span style="color: #800080;">unit</span><span class="br0">&#41;</span> == ma<span class="br0">&#41;</span>
  <span class="br0">&#125;</span>
&nbsp;
  forAll <span class="br0">&#123;</span> m =&gt; <span style="color: #2e7c0f;">identity2</span><span class="br0">&#40;</span>m<span class="br0">&#41;</span> <span class="br0">&#125;</span>
&nbsp;
  <span style="color: #999999;">// associativity</span>
  <span style="color: #140dcc;">def</span> associativity<span style="color: #7f0055;"><span class="br0">&#91;</span>A, B, C<span class="br0">&#93;</span></span><span class="br0">&#40;</span>m: M<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span>, f: A =&gt; M<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span>, g: B =&gt; M<span style="color: #7f0055;"><span class="br0">&#91;</span>C<span class="br0">&#93;</span></span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span style="color: #140dcc;">val</span> mf: M<span style="color: #7f0055;"><span class="br0">&#91;</span>B<span class="br0">&#93;</span></span> = tc.<span style="color: #2e7c0f;">bind</span><span class="br0">&#40;</span>m<span class="br0">&#41;</span><span class="br0">&#40;</span>f<span class="br0">&#41;</span>
    <span style="color: #140dcc;">val</span> mg: M<span style="color: #7f0055;"><span class="br0">&#91;</span>C<span class="br0">&#93;</span></span> = tc.<span style="color: #2e7c0f;">bind</span><span class="br0">&#40;</span>mf<span class="br0">&#41;</span><span class="br0">&#40;</span>g<span class="br0">&#41;</span>
&nbsp;
    <span style="color: #140dcc;">val</span> mg2: M<span style="color: #7f0055;"><span class="br0">&#91;</span>C<span class="br0">&#93;</span></span> = tc.<span style="color: #2e7c0f;">bind</span><span class="br0">&#40;</span>m<span class="br0">&#41;</span> <span class="br0">&#123;</span> a =&gt;
      tc.<span style="color: #2e7c0f;">bind</span><span class="br0">&#40;</span><span style="color: #2e7c0f;">f</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#40;</span>g<span class="br0">&#41;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span style="color: #2e7c0f;">assert</span><span class="br0">&#40;</span>mg == mg2<span class="br0">&#41;</span>
  <span class="br0">&#125;</span>
&nbsp;
  forAll <span class="br0">&#123;</span> <span class="br0">&#40;</span>m, f, g<span class="br0">&#41;</span> =&gt; <span style="color: #2e7c0f;">associativity</span><span class="br0">&#40;</span>m, f, g<span class="br0">&#41;</span> <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>The first two axioms (the &#8220;identity&#8221; ones) are basically saying that the <code>unit</code> function is a simple constructor with respect to the <code>bind</code> function.  Thus, when <code>bind</code> &#8220;pulls apart&#8221; the monad and passes the value to its function parameter, that value will be precisely the value that <code>unit</code> puts <em>into</em> the monad.  Likewise, if the function parameter given to <code>bind</code> simply takes the value and wraps it back up inside the monad, then the final result is exactly the same as if we had left the monad alone.</p>
<p>The third axiom is the most complicated to express, but I think it&#8217;s actually the most intuitive.  Basically, this axiom is saying that if you first <code>bind</code> with one function, then you <code>bind</code> the result against another function, that&#8217;s the same as applying the first function to the value <em>inside</em> the monad and then calling <code>bind</code> on the result of that application.  This isn&#8217;t exactly associativity in the classical sense, but you can sort of think about it in that way.</p>
<p>One of the useful consequences of the second law comes up (quite frequently) when you have one <code>bind</code> nested inside the another.  Whenever you find yourself in that situation, you can move the nested <code>bind</code> outside of the outer <code>bind</code>, flattening your code slightly.  Like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> opt: Option<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span> = <span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;string&quot;</span><span class="br0">&#41;</span>
&nbsp;
opt bind <span class="br0">&#123;</span> str =&gt; 
  <span style="color: #140dcc;">val</span> innerOpt = <span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Head: &quot;</span> + str<span class="br0">&#41;</span>
  innerOpt bind <span class="br0">&#123;</span> str =&gt; <span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span>str + <span style="color: #cb0710;">&quot; :Tail&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #999999;">// is the same as...</span>
&nbsp;
opt bind <span class="br0">&#123;</span> str =&gt; <span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Head: &quot;</span> + str<span class="br0">&#41;</span> <span class="br0">&#125;</span> bind <span class="br0">&#123;</span> str =&gt; <span style="color: #2e7c0f;">Some</span><span class="br0">&#40;</span>str + <span style="color: #cb0710;">&quot; :Tail&quot;</span><span class="br0">&#41;</span> <span class="br0">&#125;</span></pre></td></tr></table></div>

<p>The rewritten code has a much more &#8220;sequential&#8221; feel (the essence of monads!) and is almost always shorter than the nested form.</p>
<p>As I said before, the axioms are very, very intuitive once you understand the abstract concept of a monad.  They may not be intuitive to <em>express</em>, but their consequences are very easy to understand and quite natural in practice.  So, don&#8217;t spend a lot of time trying to memorize the axioms.  Your time will be better spent contemplating the way that semicolon works.</p>
<h3>Conclusion</h3>
<p>Monads are <em>not</em> scary.  They are not complex, academic or esoteric.  Monads are an abstract, mathematical label affixed to a pattern found in almost all code.  <a href="http://www.youtube.com/watch?v=TaAftRgptkQ">We all use monads every day</a>.  The hardest part in understanding monads is recognizing that the hardest part isn&#8217;t so hard after all.</p>
<p>I sincerely hope that this latest, dubious venture down the well-trod path of monad exposition has proven a fruitful one.  I can say without hesitation that the understanding and perspective which comes from a solid grasp of monads is invaluable in very practical, down-to-earth coding (even in staid languages like Java!).  Monads impart an understanding of the very fabric of sequential computation and composability, and if that isn&#8217;t sufficient motivation to learn, I don&#8217;t know what is.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/ruby/monads-are-not-metaphors/feed</wfw:commentRss>
		<slash:comments>87</slash:comments>
		</item>
		<item>
		<title>Hacking Buildr: Interactive Shell Support</title>
		<link>http://www.codecommit.com/blog/java/hacking-buildr-interactive-shell-support</link>
		<comments>http://www.codecommit.com/blog/java/hacking-buildr-interactive-shell-support#comments</comments>
		<pubDate>Mon, 12 Jan 2009 07:52:51 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/java/hacking-buildr-interactive-shell-support</guid>
		<description><![CDATA[Last week, we looked at the unfortunately-unexplored topic of Scala/Java joint compilation.&#160; Specifically, we saw several different ways in which this functionality may be invoked covering a number of different tools.&#160; Among these tools was Buildr, a fast Ruby-based drop-in replacement for Maven with a penchant for simple configuration.&#160; In the article I mentioned that [...]]]></description>
			<content:encoded><![CDATA[<p>Last week, we looked at the unfortunately-unexplored topic of <a href="http://www.codecommit.com/blog/scala/joint-compilation-of-scala-and-java-sources">Scala/Java joint compilation</a>.&nbsp; Specifically, we saw several different ways in which this functionality may be invoked covering a number of different tools.&nbsp; Among these tools was Buildr, a fast Ruby-based drop-in replacement for Maven with a penchant for simple configuration.&nbsp; In the article I mentioned that Buildr doesn&#8217;t actually have support for the Scala joint compiler out of the box.&nbsp; In fact, this feature actually requires the use of a Buildr fork I&#8217;ve been using to experiment with different extensions.&nbsp; Among these extensions is a feature I&#8217;ve been wanting from Buildr for a long time: the ability to launch a pre-configured interactive shell.</p>
<p>For those coming from a primarily-Java background, the concept of an interactive shell may seem a bit foreign.&nbsp; Basically, an interactive shell &mdash; or <a href="http://en.wikipedia.org/wiki/REPL"><em>REPL</em></a>, as it is often called &mdash; is a line-by-line language interpreter which allows you to execute snippets of code with immediate result.&nbsp; This has been a common tool in the hands of dynamic language enthusiasts since the days of LISP, but has only recently found its way into the world of mainstream static languages such as Scala.</p>
<div style="text-align:center;"><img src="http://www.codecommit.com/blog/wp-content/uploads/2009/01/interactive-shells.png" alt="interactive-shells.png" border="0"/></div>
<p>One of the most useful applications of these tools is the testing of code (particularly frameworks) before the implementations are fully completed.&nbsp; For example, when working on <a href="http://github.com/djspiewak/scala-collections/tree/master/src/main/scala/com/codecommit/collection/Vector.scala">my port of Clojure&#8217;s <code>PersistentVector</code></a>, I would often spin up a Scala shell to quickly test one aspect or another of the class.&nbsp; As a minor productivity plug, <a href="http://www.zeroturnaround.com/javarebel/">JavaRebel</a> is a truly <em>invaluable</em> tool for development of this variety.</p>
<p>The problem with this pattern of work is it requires the interactive shell in question to be pre-configured to include the project&#8217;s output directory on the CLASSPATH.&nbsp; While this isn&#8217;t usually so bad, things can get very sticky when you&#8217;re working with a project which includes a large number of dependencies.&nbsp; It isn&#8217;t too unreasonable to imagine shell invocations stretching into the tens of lines, just to spin up a &#8220;quick and dirty&#8221; test tool.</p>
<p>Further complicating affairs is the fact that many projects do away with the notion of fixed dependency paths and simply allow tools like Maven or Buildr to manage the CLASSPATH entirely out of sight.&nbsp; In order to fire up a Scala shell for a project with any external dependencies, I must first manually read my <code>buildfile</code>, parsing out all of the artifacts in use.&nbsp; Then I have to grope about in my <code>~/.m2/repository</code> directory until I find the JARs in question.&nbsp; Needless to say, the productivity benefits of this technique become extremely suspect after the first or second expedition.</p>
<p>For this reason, I strongly believe that the launch of an interactive shell should be the responsibility of the tool managing the dependencies, rather than that of the developer.&nbsp; Note that Maven already has some support for shells in conjunction with certain languages (Scala among them), but it is as crude and verbose as the tool itself.&nbsp; What I really want is to be able to invoke the following command and have the appropriate shell launched with a pre-configured CLASSPATH.&nbsp; I shouldn&#8217;t have to worry about the language of my project, the location of my repository or even if the shell requires extra configuration on my platform.&nbsp; The idea is that everything should all work auto-magically:</p>
<pre style="margin-left: 15px;">$ buildr shell</pre>
<p>This is exactly what the <a href="http://github.com/djspiewak/buildr/tree/interactive-shell"><code>interactive-shell</code> branch of my Buildr fork</a> is designed to accomplish.&nbsp; Whenever the <code>shell</code> task is invoked, Buildr looked through the current project and attempts to guess the language involved.&nbsp; This guesswork is required for a number of other features, so Buildr is actually pretty accurate in this area.&nbsp; If the language in question is Groovy or Scala, then the desired shell is obvious.&nbsp; Java does not have an integrated shell, which means that the default behavior on a Java project would be to raise an error.</p>
<p>However, the benefits of interactive shells are not limited to just the latest-and-greatest languages.&nbsp; I often use a Scala shell with Java projects, and for certain things a JRuby shell as well (<code>jirb</code>).&nbsp; Thus, my interactive shell extension also provides a mechanism to allow users to override the default shell on a per-project basis:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">define <span style="color: #cb0710;">'my-project'</span> <span style="color: #140dcc;">do</span>
  shell.<span class="me1">using</span> <span style="color: #ca9925;">:clj</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>With this configuration, regardless of the language used by the compiler for &#8220;my-project&#8221;, Buildr will launch the Clojure REPL whenever the <code>shell</code> task is invoked.&nbsp; The currently supported shells and their corresponding Buildr identifiers:</p>
<ul>
<li>Clojure&#8217;s REPL &mdash; <code>:clj</code></li>
<li>Groovy&#8217;s Shell &mdash; <code>:groovysh</code></li>
<li>JRuby&#8217;s IRB &mdash; <code>:jirb</code></li>
<li>Scala&#8217;s Shell &mdash; <code>:scala</code></li>
</ul>
<p>It is also possible to explicitly launch a specific shell.&nbsp; This is useful for situations where you might want to use the Scala shell for testing some things and the JRuby IRB for quickly prototyping other ideas (I do this a lot).&nbsp; The command to launch the JIRB shell in the context of <code>my-project</code> would be as follows:</p>
<pre style="margin-left: 15px;">$ buildr my-project:shell:jirb</pre>
<p>As a special value-added feature, all of these shells (except for Groovy&#8217;s, which is weird) will be automatically configured to use JavaRebel for the project compilation target classes if it can be automatically detected.&nbsp; This detection is performed by examining <code>REBEL_HOME</code>, <code>JAVA_REBEL</code>, <code>JAVAREBEL</code> and <code>JAVAREBEL_HOME</code> environment variables in order.&nbsp; If any one of these points to a directory which contains <code>javarebel.jar</code> or points directly to <code>javarebel.jar</code> itself, the configuration is assumed and the respective shell invocation is appropriately modified.</p>
<div style="text-align:center;"><img src="http://www.codecommit.com/blog/wp-content/uploads/2009/01/javarebel-integration.png" alt="javarebel-integration.png" border="0"/></div>
<p>Best of all, this support is implemented using a highly-extensible framework similar to Buildr&#8217;s own <code>Compiler</code> API.&nbsp; It&#8217;s very easy for plugin implementors or even average developers to simply drop-in a new shell provider, perhaps for an internal language or even some unexpected application.&nbsp; The core functionality of shell detection is integrated into Buildr itself, but this in no way hampers extensibility.&nbsp; For example, I could easily create a third-party <code>.rake</code> plugin for Buildr which added support for a whole new language (e.g. Haskell).&nbsp; In this plugin, I could also define a new shell provider which would be the default for projects using that language (e.g. GHCi).</p>
<h3>Open Question</h3>
<p>The good news is that this feature <a href="http://www.nabble.com/Interactive-Shell-Support-td21273331.html">has been discussed extensively</a> on the <code>buildr-user</code> mailing-list and the prevailing opinion seems to be that it should be folded into the main Buildr distribution.&nbsp; Exactly what form this will take has yet to be decided.&nbsp; The bad news is that there is still some dispute about a fundamental aspect of this feature&#8217;s operation.</p>
<p>The question revolves around what the exact behavior should be when the <code>shell</code> task is invoked.&nbsp; Should Buildr detect the project (or sub-project) you are in and automatically configure the shell&#8217;s CLASSPATH accordingly?&nbsp; This would give the interactive shell access to different classes depending on the current working directory.&nbsp; Alternatively, should there be one all-powerful shell per-<code>buildfile</code> configured at the root level?&nbsp; This would allow your shell to remain consistent throughout the project, regardless of your current directory.&nbsp; However, it would also mean that some configuration would be required in order to enable the functionality.&nbsp; (more details of this debate can be found <a href="http://www.nabble.com/Interactive-Shell-Support-td21273331.html">on the mailing-list</a>).</p>
<p>Additionally, what should the exact syntax be for invoking a specific shell?&nbsp; Rake 0.8 allows tasks to take parameters enclosed within square brackets.&nbsp; Thus, the syntax would be something more like the following:</p>
<pre style="margin-left: 15px">$ buildr collection:shell[jirb]</pre>
<p>In some sense, this is more logical since it reflects the fact that a single task, <code>shell</code>, is taking care of the work of invoking stuff.&nbsp; On the other hand, it&#8217;s a little less consistent with the rest of Buildr&#8217;s tasks, particularly things like &#8220;<code>test:TestClass</code>&#8221; and so on.&nbsp; This too is a matter which has yet to be settled.</p>
<p>All in all, this is a pretty experimental branch which is very open (and desirous) of outside input.&nbsp; How would you use a feature like this?&nbsp; Is there anything missing from what I have presented?&nbsp; What design path should be we take with regards to project-local vs global shell configurations?</p>
<p>If you feel like adding your voice to the chorus, feel free to leave a comment or (better yet) post a reply on the mailing-list thread.&nbsp; You&#8217;re also perfectly free to fork my remote branch at GitHub to better experiment with things yourself.&nbsp; The root of the whole plate of spaghetti is the <code>lib/buildr/shell.rb</code> file.&nbsp; <em>Bon appetit!</em></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/java/hacking-buildr-interactive-shell-support/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Integrating Scala into JRuby</title>
		<link>http://www.codecommit.com/blog/ruby/integrating-scala-into-jruby</link>
		<comments>http://www.codecommit.com/blog/ruby/integrating-scala-into-jruby#comments</comments>
		<pubDate>Mon, 29 Sep 2008 07:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/ruby/integrating-scala-into-jruby</guid>
		<description><![CDATA[More and more projects (especially startups) have been choosing to build their software in multiple languages.&#160; Rather than using SQL for the database, XML for the RPC and Java for the everything else, companies have learned that sometimes a different language can serve best in a specific area.&#160; Ola Bini provides some guidance with regards [...]]]></description>
			<content:encoded><![CDATA[<p>More and more projects (especially startups) have been choosing to build their software in multiple languages.&#160; Rather than using SQL for the database, XML for the RPC and Java for the <i>everything</i> else, companies have learned that sometimes a different language can serve best in a specific area.&#160; Ola Bini <a href="http://ola-bini.blogspot.com/2008/01/language-explorations.html">provides some guidance</a> with regards to methodology in this area in the form of what he calls &#8220;language layers&#8221;.&#160; He suggests that an application can be divided logically into separate layers, and for each of these layers there exists a class of language &#8211; be it dynamic, static, compiled or otherwise &#8211; which can best accomplish the task at hand.&#160; All of that aside, there is one problem which is absolutely assured when dealing with polyglot programming: integration between the languages.</p>
<p>I&#8217;m told that this issue came up at the JVM languages conference this past week.&#160; The problem of integrating very separate languages in a natural way is not an easy one, even when all languages in question are on the JVM.&#160; So far, the only solution that anyone has been able to come up with is just to use Java as an intermediary.&#160; After all, JRuby integrates with Java, as does Clojure, therefore JRuby has at least <em>some</em> path of integration with Clojure and vice versa.</p>
<p>The problem is that such integration is not really idiomatic.&#160; As the title of this post implies, we&#8217;re going to consider the example of Scala and JRuby.&#160; I&#8217;ve already written about how to create a Scala DSL for calling JRuby directly, so let&#8217;s look at the other side of the problem: it&#8217;s certainly <em>possible</em> to use Scala classes from within JRuby, but it isn&#8217;t exactly a pleasant proposition.&#160; Let&#8217;s imagine that I want to make use of <a href="http://www.codecommit.com/blog/misc/implementing-persistent-vectors-in-scala/final/Vector.scala">my Scala port</a> of Rich Hickey&#8217;s excellent <a href="http://www.codecommit.com/blog/scala/implementing-persistent-vectors-in-scala">immutable persistent vector</a> data structure:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">import com.<span class="me1">codecommit</span>.<span class="me1">collection</span>.<span class="me1">Vector</span>
&nbsp;
vec = Vector.<span class="me1">new</span>
vec = vec.<span class="me1">send</span><span class="br0">&#40;</span><span style="color: #cb0710;">'$plus'</span>.<span class="me1">to_sym</span>, <span style="color: #cb0710;">1</span><span class="br0">&#41;</span>
vec = vec.<span class="me1">update</span><span class="br0">&#40;</span><span style="color: #cb0710;">0</span>, <span style="color: #cb0710;">2</span><span class="br0">&#41;</span>
&nbsp;
puts vec.<span class="me1">apply</span> <span style="color: #cb0710;">0</span>       <span style="color: #999999;"># 2</span></pre></td></tr></table></div>

<p>Straightforward, but ugly.&#160; Let&#8217;s break this down a little bit&#8230;&#160; The import and instantiation are both self-explanatory, so we come to the rather cryptic invocation of <code>send</code>.&#160; In case you don&#8217;t know, this is a Ruby method which makes it possible to invoke arbitrary methods on a given object, including those with illegal characters.&#160; I happen to know that Scala will compile the <code>+</code> operator to a method named <code>$plus</code> within class <code>Vector</code>.&#160; JRuby is perfectly happy to handle and forward this call as necessary, despite the fact that you could never actually declare this method in pure Ruby (<code>$</code> has special meaning).&#160; Thus, this invocation is the same as the following Scala snippet:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala">vec = vec + <span style="color: #cb0710;">1</span></pre></td></tr></table></div>

<p>In other words, append <code>1</code> to the vector and assign the resulting <em>new</em> vector back into <code>vec</code>.</p>
<p>Moving on, we come to the invocations of <code>update</code> and <code>apply</code>.&#160; These should be a bit more comprehensible to those familiar with Scala&#8217;s intricacies.&#160; In short, these methods are how you overload the parentheses and parentheses/assignment operators.&#160; Thus, our last two lines correspond as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala">vec = <span class="br0">&#40;</span><span style="color: #2e7c0f;">vec</span><span class="br0">&#40;</span><span style="color: #cb0710;">0</span><span class="br0">&#41;</span> = <span style="color: #cb0710;">2</span><span class="br0">&#41;</span>
<span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #2e7c0f;">vec</span><span class="br0">&#40;</span><span style="color: #cb0710;">0</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p>This was just a trivial example, you can imagine how a more complicated Scala API could be neigh unusable in JRuby.&#160; Intuitively though, it shouldn&#8217;t have to be this way.&#160; After all, JRuby supports many of the same syntactic power as Scala: it has some operator overloading, closures and even more complicated features like mixins.&#160; With all of this common ground, surely there is a way to make the two coexist more naturally?&#160; I mean, optimally we could have something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">import com.<span class="me1">codecommit</span>.<span class="me1">collection</span>.<span class="me1">Vector</span>
&nbsp;
vec = Vector.<span class="me1">new</span>
vec = vec + <span style="color: #cb0710;">1</span>
vec = <span class="br0">&#40;</span>vec<span class="br0">&#91;</span><span style="color: #cb0710;">0</span><span class="br0">&#93;</span> = <span style="color: #cb0710;">2</span><span class="br0">&#41;</span>       <span style="color: #999999;"># problematic</span>
&nbsp;
puts vec<span class="br0">&#91;</span><span style="color: #cb0710;">0</span><span class="br0">&#93;</span>              <span style="color: #999999;"># 2</span></pre></td></tr></table></div>

<p>What we have just done is informally define a desired syntax for a domain-specific problem.&#160; Does that sound like an internal DSL to anyone else?</p>
<p>Our goal is to create a simple Ruby API that can be <code>require</code>d into any JRuby application to improve the integration with Scala.&#160; To do this, we will need to find a way to convert Ruby features into their corresponding Scala features by going through Java.&#160; Once we find this translation, we can use meta-programming and dangerously-cool Monkey Patching to augment JRuby&#8217;s existing Java integration.&#160; In this way, we don&#8217;t have to start our integration layer from scratch, we can just &#8220;pretty up&#8221; JRuby&#8217;s existing features, taking advantage of the fact that Scala classes <em>are</em> Java classes.</p>
<h3>Step One: Operators</h3>
<p>From our example above, converting the invocation of a <code>$plus</code> method into a Ruby <code>+</code> operator seems to be the easiest challenge to tackle.&#160; Ruby does support operator overloading; unfortunately, this support is incredibly limited when compared with Scala&#8217;s awesome power.&#160; For example, in Scala it is possible to invent arbitrary operators, a feature which is heavily used in the Scala standard library.&#160; Ruby has no such capability, operators are implemented using conventional techniques and hard-coded rules in the parser.</p>
<p>In order to avoid blowing this experiment completely out of proportion, we&#8217;re only going to implement translations for the <a href="http://phrogz.net/ProgrammingRuby/language.html#table_18.4">set of conventional Ruby operators</a>.&#160; It would be <em>possible</em> to also implement Scala operators which are made by combining existing Ruby operators (e.g. the <code>++</code> operator) using techniques developed for the <a href="http://jicksta.com/posts/superators-add-new-operators-to-ruby">Superators gem</a>, but to do so would be extremely difficult.</p>
<p>We saw in our original example that the Scala + operator is translated into an invocation of the $plus method.&#160; It stands to reason that we could make use of this fact in our translation from Ruby to Scala.&#160; The trick is finding a comprehensive list of Scala&#8217;s operators and what methods they compile to.&#160; Fortunately, I had already investigated these translations for a different project:</p>
<p align="center">
<table border="1">
<tbody>
<tr>
<td><b>Scala Operator</b></td>
<td><b>Compiles To</b></td>
</tr>
<tr>
<td><code>=</code></td>
<td><code>$eq</code></td>
</tr>
<tr>
<td><code>&gt;</code></td>
<td><code>$greater</code></td>
</tr>
<tr>
<td><code>&lt;</code></td>
<td><code>$less</code></td>
</tr>
<tr>
<td><code>+</code></td>
<td><code>$plus</code></td>
</tr>
<tr>
<td><code>-</code></td>
<td><code>$minus</code></td>
</tr>
<tr>
<td><code>*</code></td>
<td><code>$times</code></td>
</tr>
<tr>
<td><code>/</code></td>
<td><code>div</code></td>
</tr>
<tr>
<td><code>!</code></td>
<td><code>$bang</code></td>
</tr>
<tr>
<td><code>@</code></td>
<td><code>$at</code></td>
</tr>
<tr>
<td><code>#</code></td>
<td><code>$hash</code></td>
</tr>
<tr>
<td><code>%</code></td>
<td><code>$percent</code></td>
</tr>
<tr>
<td><code>^</code></td>
<td><code>$up</code></td>
</tr>
<tr>
<td><code>&amp;</code></td>
<td><code>$amp</code></td>
</tr>
<tr>
<td><code>~</code></td>
<td><code>$tilde</code></td>
</tr>
<tr>
<td><code>?</code></td>
<td><code>$qmark</code></td>
</tr>
<tr>
<td><code>|</code></td>
<td><code>$bar</code></td>
</tr>
<tr>
<td><code>\</code></td>
<td><code>$bslash</code></td>
</tr>
</tbody>
</table>
<p>Repeated iterations of an operator become corresponding repetitions of the equivalent character sequence.&#160; Thus, <code>++</code> becomes <code>$plus$plus</code>.&#160; This suggests a very natural strategy for converting Ruby operator invocations into Scala: string substitution.&#160; We can easily Ruby operator calls using method_missing, substitute the appropriate character sequences and then attempt the modified call against the same object.&#160; This idea, when translated into Ruby, is almost as simple as that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">OPERATORS = <span class="br0">&#123;</span><span style="color: #cb0710;">&quot;=&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$eq&quot;</span>, <span style="color: #cb0710;">&quot;&gt;&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$greater&quot;</span>, <span style="color: #cb0710;">&quot;&lt;&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$less&quot;</span>, \
        <span style="color: #cb0710;">&quot;+&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$plus&quot;</span>, <span style="color: #cb0710;">&quot;-&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$minus&quot;</span>, <span style="color: #cb0710;">&quot;*&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$times&quot;</span>, <span style="color: #cb0710;">&quot;/&quot;</span> =&gt; <span style="color: #cb0710;">&quot;div&quot;</span>, \
        <span style="color: #cb0710;">&quot;!&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$bang&quot;</span>, <span style="color: #cb0710;">&quot;@&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$at&quot;</span>, <span style="color: #cb0710;">&quot;#&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$hash&quot;</span>, <span style="color: #cb0710;">&quot;%&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$percent&quot;</span>, \
        <span style="color: #cb0710;">&quot;^&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$up&quot;</span>, <span style="color: #cb0710;">&quot;&amp;&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$amp&quot;</span>, <span style="color: #cb0710;">&quot;~&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$tilde&quot;</span>, <span style="color: #cb0710;">&quot;?&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$qmark&quot;</span>, \
        <span style="color: #cb0710;">&quot;|&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$bar&quot;</span>, <span style="color: #cb0710;">&quot;<span class="es0">\\</span>&quot;</span> =&gt; <span style="color: #cb0710;">&quot;$bslash&quot;</span><span class="br0">&#125;</span>
&nbsp;
alias_method <span style="color: #ca9925;">:__old_method_missing_in_scala_rb__</span>, <span style="color: #ca9925;">:method_missing</span>
<span style="color: #140dcc;">def</span> method_missing<span class="br0">&#40;</span>sym, *args<span class="br0">&#41;</span>
  str = sym.<span class="me1">to_s</span>
&nbsp;
  str = $&amp;<span class="br0">&#91;</span><span style="color: #cb0710;">1</span><span class="br0">&#93;</span> + <span style="color: #cb0710;">'_='</span> <span style="color: #140dcc;">if</span> str =~ /^<span class="br0">&#40;</span>.<span class="me1">*</span><span class="br0">&#91;</span>^\<span class="br0">&#93;</span>=<span class="br0">&#93;</span><span class="br0">&#41;</span>=$/
&nbsp;
  OPERATORS.<span class="me1">each</span> <span style="color: #140dcc;">do</span> |from, to|
    str.<span class="me1">gsub</span>!<span class="br0">&#40;</span>from, to<span class="br0">&#41;</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  <span style="color: #140dcc;">if</span> methods.<span class="me1">include</span>? str
    send<span class="br0">&#40;</span>str.<span class="me1">to_sym</span>, args<span class="br0">&#41;</span>
  <span style="color: #140dcc;">else</span>
    __old_method_missing_in_scala_rb__<span class="br0">&#40;</span>sym, args<span class="br0">&#41;</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Right at the end, after we have transformed the method name to convert any Ruby operators into their Scala equivalents, we actually check to see if the method exists instead of blindly sending the invocation.&#160; The reason for this is to avoid creating an infinite loop in cases where a method really doesn&#8217;t exist.&#160; We can rely on the presence of bona fide methods due to the way that JRuby proxies Java objects into Scala.</p>
<p>Astute readers will notice the extra bit of regular expression checking right at the head of the method.&#160; We didn&#8217;t cover this in our target example, but this transformation is none-the-less quite important.&#160; Both Scala and Ruby provide mechanisms to simulate assignment to class members through method calls.&#160; In Ruby, you just suffix the method name with <code>=</code>, whereas in Scala the correct suffix is <code>_=</code>.&#160; This extra transformation looks for those situations and converts to the appropriate result.&#160; Thus, Scala <code>var</code> fields are now accessible within JRuby using the same syntax as standard <code>attr_reader</code>/<code>attr_writer</code> methods in Ruby.</p>
<p>Two other operators which might be useful to enable are the <code>[]</code> and <code>[]=</code> operators, generally used for collections access.&#160; Scala doesn&#8217;t actually support these operators, reserving square brackets for declaring type parameters.&#160; However, it <em>does</em> provide a very similar syntax with parentheses.&#160; As a refresher, here is how we might use an array within Scala:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> arr = <span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">Array</span><span class="br0">&#40;</span><span style="color: #cb0710;">5</span><span class="br0">&#41;</span>
<span style="color: #2e7c0f;">arr</span><span class="br0">&#40;</span><span style="color: #cb0710;">0</span><span class="br0">&#41;</span> = <span style="color: #cb0710;">5</span>
<span style="color: #2e7c0f;">arr</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span><span class="br0">&#41;</span> = <span style="color: #cb0710;">4</span>
<span style="color: #2e7c0f;">arr</span><span class="br0">&#40;</span><span style="color: #cb0710;">2</span><span class="br0">&#41;</span> = <span style="color: #cb0710;">3</span>
<span style="color: #2e7c0f;">arr</span><span class="br0">&#40;</span><span style="color: #cb0710;">3</span><span class="br0">&#41;</span> = <span style="color: #cb0710;">2</span>
<span style="color: #2e7c0f;">arr</span><span class="br0">&#40;</span><span style="color: #cb0710;">4</span><span class="br0">&#41;</span> = <span style="color: #cb0710;">1</span>
&nbsp;
<span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #2e7c0f;">arr</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>        <span style="color: #999999;">// 4</span></pre></td></tr></table></div>

<p>At compile-time, this translates into corresponding invocations of the <code>apply</code> and <code>update</code> methods of class <code>Array</code>.&#160; Specifically, <code>apply</code> is used to retrieve data, while <code>update</code> is used to assign it.&#160; These are the direct corollaries to Ruby&#8217;s <code>[]</code> and <code>[]=</code>.&#160; It would only be natural to translate invocations of these operators into corresponding calls to apply and update, and we can do this simply by extending our method_missing just a little bit:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">  <span style="color: #999999;"># ...</span>
&nbsp;
  gen_with_args = proc <span style="color: #140dcc;">do</span> |name, args|
    code = <span style="color: #cb0710;">&quot;#{name}(&quot;</span>
&nbsp;
    <span style="color: #140dcc;">unless</span> args.<span class="me1">empty</span>?
      <span style="color: #140dcc;">for</span> i <span style="color: #140dcc;">in</span> <span style="color: #cb0710;">0</span>..<span class="me1"><span class="br0">&#40;</span>args</span>.<span class="me1">size</span> - <span style="color: #cb0710;">2</span><span class="br0">&#41;</span>
        code += <span style="color: #cb0710;">&quot;args[#{i}], &quot;</span>
      <span style="color: #140dcc;">end</span>
      code += <span style="color: #cb0710;">&quot;args[#{args.size - 1}]&quot;</span>
    <span style="color: #140dcc;">end</span>
&nbsp;
    code += <span style="color: #cb0710;">')'</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  <span style="color: #140dcc;">if</span> str == <span style="color: #cb0710;">'[]'</span>
    eval<span class="br0">&#40;</span>gen_with_args.<span class="me1">call</span><span class="br0">&#40;</span><span style="color: #cb0710;">'apply'</span>, args<span class="br0">&#41;</span><span class="br0">&#41;</span>
  <span style="color: #140dcc;">elsif</span> sym.<span class="me1">to_s</span> == <span style="color: #cb0710;">'[]='</span>
    eval gen_with_args.<span class="me1">call</span><span class="br0">&#40;</span><span style="color: #cb0710;">'update'</span>, args<span class="br0">&#41;</span>            <span style="color: #999999;"># doesn't work right</span>
  <span style="color: #140dcc;">elsif</span> methods.<span class="me1">include</span>? str
    send<span class="br0">&#40;</span>str.<span class="me1">to_sym</span>, args<span class="br0">&#41;</span>
  <span style="color: #140dcc;">else</span>
    __old_method_missing_in_scala_rb__<span class="br0">&#40;</span>sym, args<span class="br0">&#41;</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>The gen_with_args proc is merely a nifty little utility closure used to cut down on redundancy without creating an entire method.&#160; It actually generates a String of Ruby code which invokes the specified method with the given arguments.&#160; This is required because JRuby&#8217;s Java integration is only so smart.&#160; It will <em>try</em> to properly convert invocations of <em>n</em>-arity Java methods when Ruby arrays are passed as arguments, but it can only do so well.&#160; The safest route is to just call the method, passing each element of the array as a successive argument.</p>
<p>All of this looks quite nice, and it seems to satisfy our requirements, but there is just one problem: Ruby doesn&#8217;t behave itself with respect to the <code>[]=</code> operator.&#160; Rather than taking the sensible approach and allowing the receiving class to define its return value, it actually <em>ignores</em> whatever value is returned from the <code>[]=</code> method and forces it to be the final argument.&#160; In other words, the above code will work, but it might not integrate with Scala in quite the way we would expect.&#160; Consider our original motivating example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">import com.<span class="me1">codecommit</span>.<span class="me1">collection</span>.<span class="me1">Vector</span>
&nbsp;
vec = Vector.<span class="me1">new</span>
vec = vec + <span style="color: #cb0710;">1</span>
vec = <span class="br0">&#40;</span>vec<span class="br0">&#91;</span><span style="color: #cb0710;">0</span><span class="br0">&#93;</span> = <span style="color: #cb0710;">2</span><span class="br0">&#41;</span>       <span style="color: #999999;"># problematic</span>
&nbsp;
puts vec<span class="br0">&#91;</span><span style="color: #cb0710;">0</span><span class="br0">&#93;</span>              <span style="color: #999999;"># 2</span></pre></td></tr></table></div>

<p>Well, I said that this would be problematic.&#160; The issue is the value of the expression <code>vec[0] = 2</code> is not a new <code>Vector</code> instance as returned by the <code>Vector#update</code> method, but actually the <code>Fixnum</code> value <code>2</code>.&#160; Thus, the snippet above can <em>never</em> work.&#160; In what is probably the most bone-headed feature of the entire language, Ruby forces every invocation of <code>[]=</code> to return the assigned value.&#160; This works fine (sort-of) for mutable, side-effecting implementations like <code>Array</code> and Scala&#8217;s <code>ListBuffer</code>, but it completely falls apart for immutable, functional collections like <code>Vector</code>.&#160; So much for a language that &#8220;makes me smile&#8221;&#8230;</p>
<p>The solution of course is to always be aware of these cases where the <code>update</code> method does <em>not</em> return <code>Unit</code> and just use <code>update</code> directly rather than <code>[]=</code>.&#160; Thus, a working version of the given snippet would be as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">import com.<span class="me1">codecommit</span>.<span class="me1">collection</span>.<span class="me1">Vector</span>
&nbsp;
vec = Vector.<span class="me1">new</span>
vec = vec + <span style="color: #cb0710;">1</span>
vec = vec.<span class="me1">update</span><span class="br0">&#40;</span><span style="color: #cb0710;">0</span>, <span style="color: #cb0710;">2</span><span class="br0">&#41;</span>
&nbsp;
puts vec<span class="br0">&#91;</span><span style="color: #cb0710;">0</span><span class="br0">&#93;</span>              <span style="color: #999999;"># 2</span></pre></td></tr></table></div>

<p>By calling <code>update</code> directly, we ensure that its return value is captured and assigned back into <code>vec</code>.&#160; Kind of ugly, but unavoidable.</p>
<h3>Step Two: Mixins</h3>
<p>Mixins are probably the most useful and code-saving feature in the entire Scala language.&#160; Like interfaces, they provide the flexibility of multiple inheritance, but they also bring with that the DRYness of shared definitions between subtypes.&#160; In fact, it may even be safe to say that the overwhelming power of the Scala collections library is owed primarily to traits.&#160; After all, without the inherited method definitions from <code>Iterable</code>, each collection would have to re-implement <code>foldLeft</code>, <code>map</code>, <code>flatMap</code> and each of the myriad of common methods defined by collections.</p>
<p>It just so happens that Ruby also has mixins of a very similar variety.&#160; It seems natural then that we should be able to use Scala mixins within JRuby just as if they were standard Ruby modules.&#160; Unfortunately, Java does not have mixins, meaning that unlike operator overloading, there isn&#8217;t a nice and easy technique we can use to convert between the two worlds.&#160; The good news is that JRuby does give us a <em>bit</em> of a head start in making this integration work: it&#8217;s interface implementation syntax.</p>
<p>Ruby doesn&#8217;t support interfaces directly, but JRuby allows us to use the standard <code>include</code> syntax on a Java interface.&#160; Once we have included the interface and implemented its methods, we can pass an instance of our Ruby class into Java methods expecting instances of that interface.&#160; JRuby takes care of all of the magic required to proxy the method calls.&#160; Syntactically, it looks something like this</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">class</span> DoSomething
  include java.<span class="me1">lang</span>.<span class="me1">Runnable</span>
&nbsp;
  <span style="color: #140dcc;">def</span> run
    puts <span style="color: #cb0710;">'Running!'</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>This is how we want our mixin integration to work.&#160; We <em>should</em> be able to <code>include</code> a Scala trait into a Ruby class, implement any abstract methods and then expect everything to work per-normal.&#160; In order to accomplish this, we&#8217;re going to need to override the <code>include</code> method to check to see if its target is a Scala trait.&#160; If that is the case, then <code>include</code> should create proxies for all of the defined methods within the trait.&#160; Basically, we have to do the same thing for Scala traits that Ruby does automatically for its modules.</p>
<p>Fortunately, this too is an area where Ruby&#8217;s notion of &#8220;open classes&#8221; will come to our rescue.&#160; Not only does Ruby allow us to add methods to classes at runtime, it also allows us to <em>redefine</em> core methods within the standard library; in this case, <code>Module#include</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">class</span> <span style="color: #140dcc;">Module</span>
  alias_method <span style="color: #ca9925;">:__old_include_in_scala_rb__</span>, <span style="color: #ca9925;">:include</span>
  <span style="color: #140dcc;">def</span> include<span class="br0">&#40;</span>*modules<span class="br0">&#41;</span>
    modules.<span class="me1">each</span> <span style="color: #140dcc;">do</span> |m|
      clazz = <span style="color: #857d1f;">nil</span>
      <span style="color: #140dcc;">begin</span>
        <span style="color: #140dcc;">if</span> m.<span class="me1">java_class</span>.<span class="me1">interface</span>?
          cl = m.<span class="me1">java_class</span>.<span class="me1">class_loader</span>
          mixin_methods_for_trait<span class="br0">&#40;</span>cl, cl.<span class="me1">loadClass</span><span class="br0">&#40;</span>m.<span class="me1">java_class</span>.<span class="me1">to_s</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
        <span style="color: #140dcc;">end</span>
      <span style="color: #140dcc;">rescue</span>
      <span style="color: #140dcc;">end</span>
&nbsp;
      <span style="color: #999999;"># ...</span>
    <span style="color: #140dcc;">end</span>
&nbsp;
    modules.<span class="me1">each</span> <span class="br0">&#123;</span>|m| __old_include_in_scala_rb__ m <span class="br0">&#125;</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>The most important thing to see here is regardless of whether or not the module in question is a trait, we still forward the inclusion onto the old definition.&#160; This is critical, because it means that JRuby is still able to create an interface proxy for that class, allowing us to pass instances of the including class into Scala and have them treated as instances of the trait in question.</p>
<p>The key to our actual inclusion of the defined methods is the way in which Scala compiles traits.&#160; Consider the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">trait</span> Test <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">method1</span><span class="br0">&#40;</span><span class="br0">&#41;</span>: <span style="color: #800080;">Unit</span>
&nbsp;
  <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">method2</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;In method2()&quot;</span><span class="br0">&#41;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">class</span> Usage <span style="color: #140dcc;">extends</span> Test <span class="br0">&#123;</span>    <span style="color: #999999;">// mixin</span>
  <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">method1</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;In method1()&quot;</span><span class="br0">&#41;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Scala will compile this into the bytecode equivalent of the following Java code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #140dcc;">public</span> <span style="color: #857d1f;">interface</span> Test <span class="br0">&#123;</span>
    <span style="color: #140dcc;">public</span> <span style="color: #857d1f;">void</span> <span style="color: #2e7c0f;">method1</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
&nbsp;
    <span style="color: #140dcc;">public</span> <span style="color: #857d1f;">void</span> <span style="color: #2e7c0f;">method2</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">public</span> <span style="color: #857d1f;">class</span> Test$class <span class="br0">&#123;</span>     <span style="color: #445fba;">// actually an inner-class of Test</span>
    <span style="color: #140dcc;">public</span> <span style="color: #140dcc;">static</span> <span style="color: #857d1f;">void</span> <span style="color: #2e7c0f;">method2</span><span class="br0">&#40;</span>Test t<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        System.<span class="me1">out</span>.<span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;In method2()&quot;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">public</span> <span style="color: #857d1f;">class</span> Usage <span style="color: #140dcc;">implements</span> Test <span class="br0">&#123;</span>
    <span style="color: #140dcc;">public</span> <span style="color: #857d1f;">void</span> <span style="color: #2e7c0f;">method1</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        System.<span class="me1">out</span>.<span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;In method1()&quot;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
&nbsp;
    <span style="color: #140dcc;">public</span> <span style="color: #857d1f;">void</span> <span style="color: #2e7c0f;">method2</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        Test$class.<span style="color: #2e7c0f;">method2</span><span class="br0">&#40;</span><span style="color: #a40d14;">this</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s very clever if you think about it.&#160; Through a bit of compile-time magic, Scala is able to keep the method definitions within traits centralized (rather than inlining everything) as well as maintain full interface-level interop with Java.&#160; It is actually possible to define a Java method which accepts as a parameter an instance of a particular trait.&#160; Since traits are interfaces, javac knows how to deal with this definition and the JVM has no problems in actually dispatching the call.</p>
<p>We can use this implementation detail to enable our mixin of traits into JRuby classes.&#160; We&#8217;re already testing within <code>include</code> to see whether or not the &#8220;module&#8221; at hand is in fact an interface, it would be a simple matter to also check for the existence of a class of the form &#8220;<code>TraitName$class</code>&#8220;.&#160; If such a class exists, we could loop through all of its <code>public</code> <code>static</code> members and create corresponding proxy methods within the including class.&#160; All of this is done within the <code>mixin_methods_for_trait</code> method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">def</span> mixin_methods_for_trait<span class="br0">&#40;</span>cl, trait_class, done=Set.<span class="me1">new</span><span class="br0">&#41;</span>
  <span style="color: #140dcc;">return</span> <span style="color: #140dcc;">if</span> done.<span class="me1">include</span>? trait_class
  done &lt;&lt; trait_class
&nbsp;
  clazz = cl.<span class="me1">loadClass</span> <span style="color: #cb0710;">&quot;#{trait_class.name}$class&quot;</span>
&nbsp;
  trait_class.<span class="me1">interfaces</span>.<span class="me1">each</span> <span style="color: #140dcc;">do</span> |i| 
    <span style="color: #140dcc;">begin</span>
      mixin_methods_for_trait<span class="br0">&#40;</span>cl, i, done<span class="br0">&#41;</span>
    <span style="color: #140dcc;">rescue</span>
    <span style="color: #140dcc;">end</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  clazz.<span class="me1">declared_methods</span>.<span class="me1">each</span> <span style="color: #140dcc;">do</span> |meth|
    mod = meth.<span class="me1">modifiers</span>
&nbsp;
    <span style="color: #140dcc;">if</span> java.<span class="me1">lang</span>.<span class="me1">reflect</span>.<span class="me1">Modifier</span>.<span class="me1">isStatic</span> mod and \
	    java.<span class="me1">lang</span>.<span class="me1">reflect</span>.<span class="me1">Modifier</span>.<span class="me1">isPublic</span> mod
      <span style="color: #663f31;">@@trait_methods</span> ||= <span class="br0">&#91;</span><span class="br0">&#93;</span>
&nbsp;
      <span style="color: #140dcc;">unless</span> meth.<span class="me1">name</span>.<span class="me1">include</span>? <span style="color: #cb0710;">'$'</span>
        module_eval <span style="color: #cb0710;">&quot;<span class="es0">\</span>
def #{meth.name}(*args, &amp;block)
  args.insert(0, self)
  args &lt;&lt; block unless block.nil?
&nbsp;
  args.map! do |a|
    if defined? a.java_object
      a.java_object
    else
      a
    end
  end
&nbsp;
  begin
    scala_reflective_trait_methods[#{@@trait_methods.size}].invoke(nil, args.to_java)
  rescue java.lang.reflect.InvocationTargetException =&gt; e
    raise e.cause.message.to_s      # TODO  change over for 1.1.4
  end
end
&quot;</span>
&nbsp;
        <span style="color: #663f31;">@@trait_methods</span> &lt;&lt; meth
      <span style="color: #140dcc;">else</span>
        define_method meth.<span class="me1">name</span> <span style="color: #140dcc;">do</span> |*args|    <span style="color: #999999;"># fallback for methods with special names</span>
          args.<span class="me1">insert</span><span class="br0">&#40;</span><span style="color: #cb0710;">0</span>, <span style="color: #857d1f;">self</span><span class="br0">&#41;</span>
&nbsp;
          <span style="color: #140dcc;">begin</span>
            meth.<span class="me1">invoke</span><span class="br0">&#40;</span><span style="color: #857d1f;">nil</span>, args.<span class="me1">to_java</span><span class="br0">&#41;</span>
          <span style="color: #140dcc;">rescue</span> java.<span class="me1">lang</span>.<span class="me1">reflectInvocationTargetException</span> =&gt; e
            raise e.<span class="me1">cause</span>.<span class="me1">message</span>.<span class="me1">to_s</span>
          <span style="color: #140dcc;">end</span>
        <span style="color: #140dcc;">end</span>
      <span style="color: #140dcc;">end</span>
    <span style="color: #140dcc;">end</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>I know, the amount of code here is a little daunting, but it&#8217;s really not that bad.&#160; Essentially, this just implements our intuition about how mixins should work in Ruby.&#160; The only thing about the algorithm that we haven&#8217;t already considered is how to deal with the super-traits of traits.&#160; Since the inheriting of method definitions is only done through static proxying, there would be no reason for Scala to compile an inherited relationship between the definition class of a sub-trait and its super-trait.&#160; To get around this problem, we explicitly check for super-interfaces and then mixin their methods <em>first</em>.&#160; This is to allow for methods which are inherited by sub-traits and then overridden.</p>
<p>The final little tidbit here is the way in which the proxy methods are created.&#160; Ruby does allow us to add methods to classes at runtime, but it is unfortunately rather restrictive in <em>how</em> those methods can be added.&#160; In a nutshell, there are two main techniques: <code>module_eval</code> and <code>define_method</code>.&#160; By using <code>define_method</code>, we are able to avoid <code>String</code> evaluation and keep our editor&#8217;s syntax-highlighting happy with our sources.&#160; Unfortunately, methods created using <code>define_method</code> have a very critical limitation: they cannot accept blocks.&#160; Thus, any mixed-in trait methods which took function values as parameters would be unusable from within Ruby.</p>
<p>This inconsistency is fixed in Ruby 1.9, but until then we still need a way of proxying higher-order Scala methods.&#160; Thus, we default to using <code>module_eval</code>.&#160; Unfortunately, this technique also comes with its own set of issues; specifically: illegal characters.&#160; As we saw previously, any Scala method with a non-alphanumeric name will have to use the <code>$</code> character to denote certain symbols.&#160; However, Ruby assigns special significance to symbols like <code>$</code> and <code>@</code>, making it impossible to use them within method names.&#160; The only way around this restriction on method names is to create such special methods using <code>define_method</code>, which brings us right back to our first set of problems.</p>
<p>The only solution I could come up with for Ruby 1.8 was to use <code>module_eval</code> by default, unless the method name contained a <code>$</code> character, in which case <code>define_method</code> would be used.&#160; With this technique, almost every case is covered.&#160; The only remaining issue would be if a method with a non-alphanumeric name took a function as a parameter.&#160; In this case, the method would be unusable from Ruby.&#160; However, even a cursory glance over the Scala standard library shows that this is an extremely rare case, one which can be safely made &#8220;difficult&#8221; if it means improving the integration in other areas (trait mixins).</p>
<p>One final unfortunate note on this method: it doesn&#8217;t work with JRuby 1.1.3 and later (the current release is 1.1.4).&#160; As reported by <a href="http://jira.codehaus.org/browse/JRUBY-2999">JRUBY-2999</a>, including two separate Java interfaces with conflicting methods will actually cause the interpreter to crash.&#160; At the time of the writing, this bug has not been resolved, either in 1.1.4 or in the latest SVN sources.&#160; Since traits quite often have methods which overlap with inherited methods, there is really no way to get around this bug.&#160; Thus, the library described in this article was developed with JRuby 1.1.2 and will only work with that release or any upcoming releases which fix the regression.&#160; Interestingly, this means that the library must deal with a <em>different</em>, less critical JRuby bug which makes it impossible to <code>raise</code> Java exceptions.&#160; As this exception bug does not entirely prevent the use of Scala mixins, it is preferable to the more serious interpreter-crash regression.</p>
<p>Now that we have mixins at our disposal, it is possible to further improve our Scala integration by implementing a <code>scala_object</code> method for <code>Array</code> and <code>Hash</code>, one which converts these objects into a form which can be passed to Scala methods expecting <code>Seq</code> and <code>Map</code>, respectively.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">class</span> BoxedRubyArray
  include Scala::RandomAccessSeq
&nbsp;
  <span style="color: #140dcc;">def</span> initialize<span class="br0">&#40;</span>delegate<span class="br0">&#41;</span>
    <span style="color: #663f31;">@delegate</span> = delegate
  <span style="color: #140dcc;">end</span>
&nbsp;
  <span style="color: #140dcc;">def</span> apply<span class="br0">&#40;</span>i<span class="br0">&#41;</span>
    <span style="color: #663f31;">@delegate</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  <span style="color: #140dcc;">def</span> length
    <span style="color: #663f31;">@delegate</span>.<span class="me1">size</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span>
&nbsp;
<span style="color: #140dcc;">class</span> Array
  <span style="color: #140dcc;">def</span> scala_object
    BoxedRubyArray.<span class="me1">new</span> <span style="color: #857d1f;">self</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>The <code>Hash</code> proxy is analogous, but much more verbose.&#160; The key to this entire implementation here is the enabling of Scala mixins within JRuby.&#160; All we have to do is mixin the <code>RandomAccessSeq</code> trait, implement <code>apply</code> and <code>length</code>, and suddenly we have a first-class Scala collection backed by a Ruby <code>Array</code>.&#160; Not only does this allow us to use some of Scala&#8217;s higher-order magic on Ruby arrays, it also enables previously impossible usages like the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">import com.<span class="me1">codecommit</span>.<span class="me1">collection</span>.<span class="me1">Vector</span>
&nbsp;
vec = Vector.<span class="me1">new</span>
vec = vec + <span style="color: #cb0710;">1</span> + <span style="color: #cb0710;">2</span> + <span style="color: #cb0710;">3</span> + <span style="color: #cb0710;">4</span> + <span style="color: #cb0710;">5</span>
&nbsp;
arr = <span class="br0">&#91;</span><span style="color: #cb0710;">6</span>, <span style="color: #cb0710;">7</span>, <span style="color: #cb0710;">8</span>, <span style="color: #cb0710;">9</span>, <span style="color: #cb0710;">0</span><span class="br0">&#93;</span>
cat = vec.<span class="me1">concat</span> arr.<span class="me1">scala_object</span></pre></td></tr></table></div>

<p>At the end of this snippet, <code>cat</code> is a Scala <code>Iterable</code> with contents <code>[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]</code>.&#160; Remember that <code>concat</code> is actually a method within <code>Vector</code> which itself expects an instance of <code>Iterable</code>.&#160; Fortunately, we now have a method to convert a Ruby array into just such an <code>Iterable</code>, which we then pass to <code>concat</code> achieving our final result.</p>
<h3>Step Three: Closures</h3>
<p>Both Ruby and Scala have this notion of closures, which are assignable, anonymous functions with access to their enclosing scope.&#160; Closures are most often used as a syntactic device for passing and/or storing a bit of code for later invocation.&#160; A common example that even Java has to deal with would be event handling, where a specific code block must be executed every time a button is pressed.&#160; Scala and Ruby take this concept a little further, providing higher-order functions like <code>map</code> and implementing conventional iteration using <code>foreach</code> and <code>each</code> (respectively).</p>
<p>Optimally, we should be able to call Scala methods passing Ruby blocks where Scala function values are required.&#160; As it happens, even without special Scala integration magic, we can already get very close to this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">vec = Vector.<span class="me1">new</span>
<span style="color: #999999;"># ...</span>
&nbsp;
vec.<span class="me1">foreach</span> <span style="color: #140dcc;">do</span> |e|
  puts e
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>This will print every element in the <code>vec</code> instance.&#160; This works because JRuby allows blocks to be passed to methods accepting interfaces.&#160; Since Scala&#8217;s function values are in fact subtypes of traits <code>Function0</code>, <code>Function1</code> and so on (according to arity), JRuby is able to recognize the method signatures appropriately and proxy the values.</p>
<p>Unfortunately, JRuby doesn&#8217;t innately understand Scala traits, so it doesn&#8217;t know that the <code>compose</code> method should be proxied to an implementation within the trait.&#160; I <em>assume</em> that JRuby will proxy every method in the target interface to the block in question (I haven&#8217;t actually tested that).&#160; Assuming this is the case, the moment that <code>foreach</code> (or any other method) attempts to invoke <code>compose</code> on a proxied JRuby block, the result will be a <code>ClassCastException</code>.&#160; It just so happens that <code>foreach</code> behaves itself and there is nothing to worry about, but we cannot make that guarantee in general.</p>
<p>The solution of course is to do for Ruby&#8217;s <code>Proc</code> what we already did for <code>Array</code> and <code>Hash</code>: provide a wrapper which uses the <code>Function</code><em>n</em> mixin and delegates to the <code>Proc</code> in question.&#160; However, unlike <code>Array</code>, there is no single &#8220;<code>Function</code>&#8221; mixin that we can use.&#160; Instead, we must create a <em>separate</em> wrapper for each function arity supported by Scala&#8230;all 22 of them.&#160; Fortunately, Ruby&#8217;s meta-programming capabilities help us out here, allowing us to define classes within a loop and then dynamically select the correct wrapper class name based on the <code>Proc</code> <a href="http://www.ruby-doc.org/core/classes/Proc.html#M001577">arity</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">module</span> ScalaProc
  <span style="color: #140dcc;">class</span> ScalaFunction
    <span style="color: #140dcc;">def</span> initialize<span class="br0">&#40;</span>delegate<span class="br0">&#41;</span>
      <span style="color: #663f31;">@delegate</span> = delegate
    <span style="color: #140dcc;">end</span>
&nbsp;
    <span style="color: #140dcc;">def</span> apply<span class="br0">&#40;</span>*args<span class="br0">&#41;</span>
      <span style="color: #663f31;">@delegate</span>.<span class="me1">call</span> args
    <span style="color: #140dcc;">end</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  <span style="color: #140dcc;">for</span> n <span style="color: #140dcc;">in</span> <span style="color: #cb0710;">0</span>..<span style="color: #cb0710;">22</span>    <span style="color: #999999;"># sneaky, but much more concise</span>
    eval <span style="color: #cb0710;">&quot;<span class="es0">\</span>
class Function#{n} &lt; ScalaFunction
  include Scala::Function#{n}
end
&quot;</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span>
&nbsp;
<span style="color: #140dcc;">class</span> Proc
  <span style="color: #140dcc;">def</span> to_function
    eval <span style="color: #cb0710;">&quot;ScalaProc::Function#{arity}.new self&quot;</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  <span style="color: #140dcc;">def</span> java_object
    to_function
  <span style="color: #140dcc;">end</span>
&nbsp;
  <span style="color: #140dcc;">def</span> scala_object
    to_function
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>The only abstract method within any of the <code>Function</code><em>n</em> traits is apply, taking the same number of arguments as the function arity.&#160; It&#8217;s easy enough to implement this function once within the <code>ScalaFunction</code> superclass, which means that all we have to do within each of the arity-specific wrappers is mixin the relevant <code>Function</code>.</p>
<p>Now that we have this conversion between Ruby <code>Proc</code>(s) and Scala function values, we can make use of it in situations where it becomes necessary.&#160; For example, let&#8217;s say that I have a Scala method like the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">doAndMultiply</span><span class="br0">&#40;</span>by: <span style="color: #800080;">Int</span><span class="br0">&#41;</span><span class="br0">&#40;</span>f: <span class="br0">&#40;</span><span style="color: #800080;">Int</span><span class="br0">&#41;</span>=&gt;Int<span class="br0">&#41;</span> = <span class="br0">&#123;</span>
  f compose <span class="br0">&#123;</span> <span class="br0">&#40;</span>_:<span style="color: #800080;">Int</span><span class="br0">&#41;</span> * by <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Simply put, this method takes two <a href="http://www.codecommit.com/blog/scala/function-currying-in-scala">curried parameters</a>, an <code>Int</code> along with a function value taking another <code>Int</code> and returning an <code>Int</code>.&nbsp; This method then returns a new function which itself takes an <code>Int</code>, first multiplies it by the original first <code>Int</code> parameter, then applies the given function to the result.&#160; Got all that?&#160; <img src='http://www.codecommit.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>We can call this function from Scala in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> comp = <span style="color: #2e7c0f;">doAndMultiply</span><span class="br0">&#40;</span><span style="color: #cb0710;">3</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> _ + <span style="color: #cb0710;">2</span> <span class="br0">&#125;</span>
<span style="color: #2e7c0f;">comp</span><span class="br0">&#40;</span><span style="color: #cb0710;">5</span><span class="br0">&#41;</span>      <span style="color: #999999;">// =&gt; 17</span></pre></td></tr></table></div>

<p>Now that we have our super-fancy <code>Proc</code> conversion, we are able to use an almost identical call syntax from within Ruby.&#160; Notice how we are able to take advantage of the way in which Scala compiles curried methods to achieve a JRuby syntax which looks almost exactly like block-standard Ruby:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">add = proc <span class="br0">&#123;</span>|x| x + <span style="color: #cb0710;">2</span> <span class="br0">&#125;</span>
comp = doAndMultiply<span class="br0">&#40;</span><span style="color: #cb0710;">3</span>, add.<span class="me1">scala_object</span><span class="br0">&#41;</span>
&nbsp;
comp.<span class="me1">call</span> <span style="color: #cb0710;">5</span>        <span style="color: #999999;"># =&gt; 17</span></pre></td></tr></table></div>

<p>And if <code>doAndMultiply</code> is a method brought in via a mixin, we can do even better:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">comp = doAndMultiply <span style="color: #cb0710;">3</span> <span style="color: #140dcc;">do</span> |x| 
  x + <span style="color: #cb0710;">2</span>
<span style="color: #140dcc;">end</span>
&nbsp;
comp.<span class="me1">call</span> <span style="color: #cb0710;">5</span>        <span style="color: #999999;"># =&gt; 17</span></pre></td></tr></table></div>

<p>This works because of the way in which we coerce parameters within the proxies for the mixed-in methods.&#160; Recall from earlier:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">args.<span class="me1">map</span>! <span style="color: #140dcc;">do</span> |a|
  <span style="color: #140dcc;">if</span> <span style="color: #140dcc;">defined?</span> a.<span class="me1">java_object</span>
    a.<span class="me1">java_object</span>
  <span style="color: #140dcc;">else</span>
    a
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>The very reason we have to explicitly map over the method arguments is to satisfy this particular case.&#160; JRuby&#8217;s Ruby-to-Java coercion is pretty smart, but it doesn&#8217;t seem to be up to this particular challenge (believe me, I tried).&#160; Thankfully, a little bit of benign check-and-coerce later, our arguments are none the worse for wear and in a form that Scala can chew on.</p>
<h3>Conclusion</h3>
<p>As you may have guessed, I have already taken the liberty of implementing the framework described in this article.&#160; It even has a few features I didn&#8217;t discuss, like auto-detecting the default Scala installation and the ability to use Scala function values as if they were Ruby <code>Proc</code>(s).&#160; All in all, it makes for a very slick way of working with Scala libraries from within JRuby scripts in an intuitive, idiomatic fashion.&#160; Hopefully, this should help to encourage the use of these two fascinating technologies together in future projects.</p>
<ul>
<li>Download <a href="http://www.codecommit.com/blog/misc/scala.rb">scala.rb</a> (does <i>not</i> work with JRuby 1.1.3 or 1.1.4)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/ruby/integrating-scala-into-jruby/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Implicit Conversions: More Powerful than Dynamic Typing?</title>
		<link>http://www.codecommit.com/blog/ruby/implicit-conversions-more-powerful-than-dynamic-typing</link>
		<comments>http://www.codecommit.com/blog/ruby/implicit-conversions-more-powerful-than-dynamic-typing#comments</comments>
		<pubDate>Mon, 15 Sep 2008 07:00:00 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/ruby/implicit-conversions-more-powerful-than-dynamic-typing</guid>
		<description><![CDATA[One of the most surprising things I&#8217;ve ever read about Scala came in the form of a (mostly positive) review article.&#160; This article went to some lengths comparing Scala to Java, JRuby on Groovy, discussing many of its advantages and disadvantages relative to those languages.&#160; Everyone seems to be writing articles to this effect these [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most surprising things I&#8217;ve ever read about Scala came in the form of a (mostly positive) review article.&#160; This article went to some lengths comparing Scala to Java, JRuby on Groovy, discussing many of its advantages and disadvantages relative to those languages.&#160; Everyone seems to be writing articles to this effect these days, so the comparison in and of itself was not surprising.&#160; What <em>was</em> interesting was an off-hand comment discussing Scala&#8217;s &#8220;dynamic typing&#8221; and how it aids in the development of domain specific languages.</p>
<p>Now this article had just finished a long-winded presentation of type inference and compilation steps, so I&#8217;m quite certain that the author was aware of Scala&#8217;s type system.&#160; The more likely target of the &#8220;dynamic typing&#8221; remark would be Scala&#8217;s implicit conversions mechanism.&#160; I have heard this language feature described many times as being a way of &#8220;dynamically&#8221; adding members to an existing class.&#160; While it would be incorrect to say that this feature constitutes a dynamic type system, it is true that it may be used to satisfy many of the same design patterns.&#160; Consider the facetious example of a string &#8220;reduction&#8221; method, one which produces an acronym based on the upper-case characters within the string:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> acronym = <span style="color: #cb0710;">&quot;Microsoft Certified Systems Engineer&quot;</span>.<span class="me1">reduce</span>
<span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span>acronym<span class="br0">&#41;</span>            <span style="color: #999999;">// MCSE</span></pre></td></tr></table></div>

<p>The immediate problem with this snippet is the fact that string literals are of type <code>java.lang.String</code>, a class which comes pre-defined by the language.&#160; The only way to ensure that the above syntax works properly is to &#8220;add&#8221; the <code>reduce</code> method to the <code>String</code> class separate from its definition.&#160; In a language such as Ruby or Groovy which have dynamic type systems, we could simply open the class definition and add a new method at runtime.&#160; However, in Scala we have to be a bit more tricky.&#160; We can&#8217;t actually add methods to an existing class, but we can define a new class which contains the desired method.&#160; Once we have that, we can define an implicit conversion from our target class to our new class.&#160; The Scala compiler sees this and performs the appropriate magic behind the scenes.&#160; In code, it looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">class</span> <span style="color: #2e7c0f;">MyRichString</span><span class="br0">&#40;</span>str: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> reduce = str.<span class="me1">toCharArray</span>.<span style="color: #2e7c0f;">foldLeft</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;&quot;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#40;</span>t, c<span class="br0">&#41;</span> =&gt;
    t + <span class="br0">&#40;</span><span style="color: #140dcc;">if</span> <span class="br0">&#40;</span>c.<span class="me1">isUpperCase</span><span class="br0">&#41;</span> c.<span class="me1">toString</span> <span style="color: #140dcc;">else</span> <span style="color: #cb0710;">&quot;&quot;</span><span class="br0">&#41;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">implicit</span> <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">str2MyRichString</span><span class="br0">&#40;</span>str: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span> = <span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">MyRichString</span><span class="br0">&#40;</span>str<span class="br0">&#41;</span></pre></td></tr></table></div>

<p>This contrasts quite dramatically with the Ruby implementation of the same concept via open classes (somewhat less-graciously known as &#8220;Monkey Patching&#8221;):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">class</span> String
  <span style="color: #140dcc;">def</span> reduce
    arr = unpack<span class="br0">&#40;</span><span style="color: #cb0710;">'c*'</span><span class="br0">&#41;</span>.<span class="me1">select</span> <span class="br0">&#123;</span> |c| <span class="br0">&#40;</span><span style="color: #cb0710;">65</span>..<span style="color: #cb0710;">90</span><span class="br0">&#41;</span>.<span class="me1">include</span>? c <span class="br0">&#125;</span>
    arr.<span class="me1">pack</span> <span style="color: #cb0710;">'c*'</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span>
&nbsp;
puts <span style="color: #cb0710;">'HyperText Transfer Protocol'</span>.<span class="me1">reduce</span>       <span style="color: #999999;"># HTTP</span></pre></td></tr></table></div>

<p>No visible type conversion is taking place here, all we did is add a method to an existing class and trust that the runtime can figure out the rest.&#160; Indeed, for this application, we don&#8217;t really need anything else.&#160; However, as anyone with experience implementing internal domain-specific languages will tell you, seldom is life as simple as adding a few methods to an existing class.&#160; Consider a more complicated scenario where we need to <em>overload</em> the <code><</code> operator on integers to operate on <code>String</code> values, returning <code>true</code> if the length of the string is less than the integer value, otherwise <code>false</code>.&#160; In Scala, we would once again make use of the implicit conversion mechanism, this time with an even more concise syntax:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">implicit</span> <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">lessThanOverload</span><span class="br0">&#40;</span>i: <span style="color: #800080;">Int</span><span class="br0">&#41;</span> = <span style="color: #140dcc;">new</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> &lt;<span class="br0">&#40;</span>str: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span> = str.<span class="me1">length</span> &lt; i
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>In fact, we don't even need to go this far.&#160; It is possible to create an implicit conversion from <code>String</code> to <code>Int</code> defined on the length of the <code>String</code>.&#160; This would allow <em>existing</em> method implementations within the <code>Int</code> class to operate upon <code>String</code> values:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">implicit</span> <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">str2Int</span><span class="br0">&#40;</span>str: <span style="color: #857d1f;">String</span><span class="br0">&#41;</span> = str.<span class="me1">length</span></pre></td></tr></table></div>

<p>As a matter of interest, this particular situation can be managed by one of the most convoluted and verbose languages on the market, C++:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="cpp"><span style="color: #857d1f;">bool</span> operator&lt;<span class="br0">&#40;</span><span style="color: #140dcc;">const</span> <span style="color: #857d1f;">int</span> &amp;i, <span style="color: #140dcc;">const</span> std::<span class="me2">string</span> &amp;str<span class="br0">&#41;</span>
<span class="br0">&#123;</span>
    <span style="color: #140dcc;">return</span> str.<span style="color: #2e7c0f;">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span> &lt; i;
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Despite the seemingly-dynamic nature of the problem, the statically typed language camp seems well represented in terms of solutions.&#160; Ironically, this sort of problem is one which will be exceedingly difficult to solve in a language like Ruby.&#160; This is primarily because method overloading is an innately <em>static</em> device.&#160; That's not to say that overloading is impossible in a dynamically typed language (Groovy), but it's not easy.&#160; To see why, let's consider the most natural implementation of our operator problem in Ruby:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">class</span> Fixnum
  <span style="color: #140dcc;">def</span> &lt;<span class="br0">&#40;</span>str<span class="br0">&#41;</span>
    str.<span class="me1">size</span> &lt; <span style="color: #857d1f;">self</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Intuitively, this may seem like the right way to approach the problem, but the results of such an implementation would be disastrous.&#160; At the very least, the first time anyone attempted to perform a <code><</code> comparison targeting an integer, the interpreter will overflow the call stack.&#160; In fact, any time <em>any</em> code uses the less-than operator on an instance of <code>Fixnum</code>, the interpreter will crash.&#160; The reason for this is the invocation of <code><</code> upon <code>str.size</code> within our "overloaded" definition.&#160; This call creates a very tight recursive loop which will very quickly eat through all available stack frames.&#160; We can avoid this problem by reversing the comparison like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">class</span> Fixnum
  <span style="color: #140dcc;">def</span> &lt;<span class="br0">&#40;</span>str<span class="br0">&#41;</span>
    <span style="color: #857d1f;">self</span> &gt;= str.<span class="me1">size</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Now we don't have to worry about stack overflow, but in the process we have accidentally redefined integer-to-integer comparison in a very strange way:</p>
<pre>irb(main):006:0&gt; 123 &lt; 'test'
=&gt; true
irb(main):007:0&gt; 123 &lt; 123
=&gt; true</pre>
<p>Clearly, more effort is going to be required if we are to put to rest our little dilemma.&#160; As it turns out, the final solution is surprisingly ugly and verbose:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">class</span> Fixnum
  alias_method <span style="color: #ca9925;">:__old_less_than__</span>, <span style="color: #cb0710;">'&lt;'</span>.<span class="me1">to_sym</span>
  <span style="color: #140dcc;">def</span> &lt;<span class="br0">&#40;</span>target<span class="br0">&#41;</span>
    <span style="color: #140dcc;">if</span> target.<span class="me1">kind_of</span>? String
      __old_less_than__ target.<span class="me1">size</span>
    <span style="color: #140dcc;">else</span>
      __old_less_than__ target
    <span style="color: #140dcc;">end</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Whatever happened to Ruby as a "more elegant" language?&#160; The unfortunate truth is that in order to <em>emulate</em> method overloading based on input type, we must hold onto the old method implementation while we implement a type-sensitive facade in its place.&#160; The <code>alias_method</code> invocation literally copies the old less-than operator implementation and provides us with a way of referencing it within our later redefinition.&#160; And what happens if someone <em>else</em> happens to monkey patch <code>Fixnum</code> and (for whatever reason) uses the identifier "<code>__old_less_than__</code>"?&#160; Well, then we have problems.&#160; It's like the old days of Lisp macros and endless identifier collisions.</p>
<p>It is true that this was an example specifically contrived to make Ruby look bad.&#160; I could have implemented the overload using Groovy's meta-classes and been reasonably certain that everything would work out fine, but that's not the point.&#160; The point is that there are a surprising number of situations where static typing serves not only to check for errors but also to allow extension patterns which would be otherwise impossible (or very, very difficult).&#160; Dynamic typing isn't the panacea of extensibility that its proponents make it out to be, sometimes it isn't quite up to the task.</p>
<p>In fact (and this is where we come to my Digg-friendly point), I would submit that Scala (and to a lesser extent, C++) have created a mechanism for controlled extensibility which is <em>more</em> powerful than Ruby's open classes design.&#160; That's not to say that there aren't situations which are easily solved using open classes and entirely intractable using only implicit conversions, but in my experience these scenarios are very rare.&#160; In fact, I believe that it is far more common to run against a problem like my contrived overload which is greatly simplified through the use of static typing.</p>
<p>Ironically enough, some of <a href="http://weblog.raganwald.com/">Ruby's greatest pundits</a> are starting to come around to the belief that a more controlled and well-defined model of class extension is required.&#160; <a href="http://parsetree.rubyforge.org/">ParseTree</a> is a Ruby framework which provides mechanisms for dynamically manipulating the AST of an expression prior to evaluation.&#160; Conceptually, it is very similar to Lisp's macros and peripherally related to .NET's expression trees (used in LINQ).&#160; ParseTree is used by a number of complex Ruby domain-specific languages, including <a href="http://ambition.rubyforge.org/">Ambition</a>, a fact which is extremely telling of how great the need is for just such a tool.&#160; Having myself attempted a domain-specific language for constructing queries, I can state categorically that to do such a thing solely on the basis of open classes would be nearly impossible.&#160; Even if successful, such a framework would be extremely volatile, sensitive to the slightest change in the Ruby core library, either caused by update or by <em>other</em> packages injecting their own meddlesome implementations into runtime classes.</p>
<p>Lex Spoon (co-author of <em>Programming in Scala</em>) <a href="http://www.infoq.com/presentations/jaoo-spoon-scala">once said</a> that any language which seriously targeted domain-specific languages would have to create some sort of implicit conversion mechanism.&#160; At the time, I was skeptical, convinced that Ruby (and similar) would always have the upper-hand in the area of class extension due to their dynamic treatment of modules and classes.&#160; However, after some serious dabbling in the field of internal domain-specific languages, I'm beginning to come 'round to his point of view.&#160; Implicit conversions are far from a weak imitation of Scala's dynamically typed "betters", they are a powerful and controlled way of extending types far beyond anything which can be easily accomplished through open classes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/ruby/implicit-conversions-more-powerful-than-dynamic-typing/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Pipe Dream: Static Analysis for Ruby</title>
		<link>http://www.codecommit.com/blog/ruby/pipe-dream-static-analysis-for-ruby</link>
		<comments>http://www.codecommit.com/blog/ruby/pipe-dream-static-analysis-for-ruby#comments</comments>
		<pubDate>Mon, 30 Jun 2008 07:00:01 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/ruby/pipe-dream-static-analysis-in-ruby</guid>
		<description><![CDATA[Yes, yes I know: Ruby is a dynamic language.&#160; The word &#8220;static&#8221; is literally opposed to everything the language stands for.&#160; With that said, I think that even Ruby development environments would benefit from some simple static analysis, just enough to catch the really idiotic errors.
Here&#8217;s the crux of the problem: people don&#8217;t test well.&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Yes, yes I know: Ruby is a dynamic language.&nbsp; The word &#8220;static&#8221; is literally opposed to everything the language stands for.&nbsp; With that said, I think that even Ruby development environments would benefit from some simple static analysis, just enough to catch the really idiotic errors.</p>
<p>Here&#8217;s the crux of the problem: people don&#8217;t test well.&nbsp; Even with nice, <a href="http://www.codecommit.com/blog/java/the-brilliance-of-bdd">behavior-driven development</a> as facilitated by frameworks like RSpec, very few developers sufficiently test their code.&nbsp; This isn&#8217;t just a problem with dynamic languages either, no one is safe from the test disability.&nbsp; In some ways, it&#8217;s a product of laziness, but I think in most cases, good developers just don&#8217;t want to work on mundane problems.&nbsp; It&#8217;s <em>boring</em> having to write unit test after unit test, checking and re-checking the same snippet of code with different input.</p>
<p>In some sense, it is this problem that compilers and static type systems try to avert, at least partially.&nbsp; The very purpose of a static type system is to be able to prove certain things about your code simply by analysis.&nbsp; By enabling the compiler to say things using the type system, the language is providing a safety net which filters out ninety percent of the annoying &#8220;no-brainer&#8221; mistakes.&nbsp; A simple example would be invoking a method with the wrong parameters; or worse yet, misspelling the name of the method or type altogether.</p>
<p>The problem is that there are some problems which are more simply expressed in ways which are not provably sound.&nbsp; In static languages, we get around this by casting, but such techniques are ugly and obviously contrived.&nbsp; It is this problem which has given rise to the kingdom of dynamic languages; it is for this reason that most scripting languages have dynamic type systems: simple expression of algorithm without worrying about provability.&nbsp; In fact, there are so many problems which do not fit nicely within most type systems that many developers have chosen to eschew static languages altogether, claiming that static typing just gets in the way.</p>
<p>Unfortunately, by abandoning static types, these languages lose that typo safety net.&nbsp; It&#8217;s too easy to make a trivial mistake in a dynamic language, buried somewhere deep in the bowls of your application.&nbsp; This mistake could easily be averted by a compiler with validating semantic analysis, but in a dynamic language, such a mistake could go unnoticed, conceivably even making it into production.&nbsp; For this reason, most dynamic language proponents are also strong advocates of solid, comprehensive testing.&nbsp; They have to be, for without such testing, one should never trust dynamic code in a production system (or any code, for that matter, but especially the unchecked dynamic variety).</p>
<p>Most large, production systems written in languages like Ruby or Groovy have large test suites which sometimes take hours to run.&nbsp; These suites are extremely fine-grained, optimally checking every line of code with every possible kind of input, so as to be sure that mistakes are caught.&nbsp; This is where the flexibility of dynamic typing really comes back to haunt you: extra testing is required to ensure that silly mistakes don&#8217;t slip through.&nbsp; The irony is that a lot of developers using dynamic languages do so to get away from the &#8220;nuisance&#8221; of compilation, when all they have done is trade one inconvenience for another (testing).</p>
<p>Given this situation, it&#8217;s not unreasonable to conclude that what dynamic languages really need is a tool which can look through code and find all of those brain-dead mistakes.&nbsp; Such a tool could be run along with the normal test suite, finding and reporting errors in much the same way.&nbsp; It wouldn&#8217;t really have to be a compiler, so the tool wouldn&#8217;t slow down the development process, it would just be an effective layer of automated white-box testing.</p>
<p>But how could such a thing be accomplished in a language like Ruby?&nbsp; After all, it is a truly dynamic language.&nbsp; Methods don&#8217;t even exist until runtime, and sometimes only if certain code paths are run.&nbsp; Types are completely undeclared, and every object can potentially respond to any method.&nbsp; The answer is to perform extremely permissive inference.</p>
<p>It was actually a recent post by James Ervin on <a href="http://iacobus.blogspot.com/2008/06/dont-call-it-static-or-dynamic-language.html">the nomenclature of type systems</a> which got me thinking along these lines.&nbsp; It should be possible by static analysis to infer the <em>structural</em> type of any value based on its usage.&nbsp; Consider:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">def</span> do_something<span class="br0">&#40;</span>obj<span class="br0">&#41;</span>
  <span style="color: #140dcc;">if</span> obj.<span class="me1">to_i</span> == <span style="color: #cb0710;">0</span>
    obj<span class="br0">&#91;</span><span style="color: #ca9925;">:test</span><span class="br0">&#93;</span>
  <span style="color: #140dcc;">else</span>
    other = obj.<span class="me1">find</span> <span style="color: #ca9925;">:name</span> =&gt; <span style="color: #cb0710;">'Daniel'</span>
    other.<span class="me1">to_s</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Just by examining this code, we can say certain things about the types involved.&nbsp; For instance, we know that <code>obj</code> must respond to the following methods:</p>
<ul>
<li><code>to_i</code>
<li><code>[Symbol]</code>
<li><code>find(Hash)</code></li>
</ul>
<p>In turn, we know that the <code>find(Hash)</code> method must return a value which defines <code>to_s</code>.&nbsp; Of course, this last bit of information isn&#8217;t very useful, because every object defines that method, but it&#8217;s still worth the inference.&nbsp; The really useful inference which comes out of <code>to_s</code> is the knowledge that this method <em>sometimes</em> returns a value of type <code>String</code> (making the assumption that <code>to_s</code> hasn&#8217;t been redefined to return a different type, which isn&#8217;t exactly a safe assumption).&nbsp; At other times, <code>do_something</code> will return whatever value comes from the square bracket operator (<code>[]</code>) on <code>obj</code>.&nbsp; This bit of information we must remember in the analysis.&nbsp; We can&#8217;t just assume that this method will return a <code>String</code> all the time, even if <code>to_s</code> does because method return types need not be homogeneous in dynamic languages.</p>
<p>Now, at this point we have effectively built up a structural type which is accepted by <code>do_something</code>.&nbsp; Literally, we have formalized in the analysis what our intuition has already told us about the method.&nbsp; There are some gaps, but that is to be expected.&nbsp; The key to this analysis is <em>not</em> attempting to be comprehensive.&nbsp; Dynamic languages cannot be analyzed as if they were static, one must expect to have certain limitations.&nbsp; In such situations where the analysis is insufficient, it must assume that the code is valid, otherwise there will be thousands of false positives in the error checking.</p>
<p>So what is it all good for?&nbsp; Well, imagine that somewhere else in our application, we have the following bit of code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">do_something <span style="color: #cb0710;">42</span></pre></td></tr></table></div>

<p>This is something we <em>know</em> will fail, because we have a simple value (42) which has a nominal type we can easily infer.&nbsp; A little bit of checking on this type reveals the fact that it does not define square brackets, nor does it define a <code>find(Hash)</code> method.&nbsp; This finding could be reported as an error by the analysis engine.</p>
<p>Granted, we still have to account for the fact that Ruby has things like <code>method_missing</code> and open classes, but all of this can fall into the fuzzy area of the analysis.&nbsp; In situations where it <em>might</em> be alright to pass an object which does not satisfy a certain aspect of the structural type, the analysis must let it pass without question.</p>
<p>You can imagine how this analysis could traverse the entire source tree, making the strictest inferences it can and allowing for dynamic fuzziness where applicable.&nbsp; Since the full sources of every Ruby function, class and module are available at runtime, analysis could be performed without any undue concern regarding obfuscation or parsing of binaries.&nbsp; Conceivably, most trivial errors could be caught without any tests being written, taking some of the burden off of the developer.&nbsp; There is a slight concern that developers would build up a false sense of security regarding their testing (or lack thereof), but I think we just have to trust that won&#8217;t happen, or won&#8217;t last long if it does.</p>
<p>Most advanced Ruby toolsets already have an analysis somewhat similar to the one I outlined.&nbsp; NetBeans Ruby for example has some fairly advanced nominal type inference to allow things like semantic highlighting and content assist.&nbsp; But as far as I know, this type inference is only nominal, and fairly local at that.&nbsp; The structural type inference that I am proposing could conceivably provide far better assurances and capabilities than mere nominal inference, especially if enhanced through successive iteration and a more &#8220;global&#8221; approach (similar to Hindley/Milner in static languages).</p>
<p>One thing is certain, it isn&#8217;t working to just rely on developers being conscientious with their testing.&nbsp; With the rapid rise in production systems running on dynamic languages, it is in all of our best interests to try to find a way to make these systems more stable and reliable.&nbsp; The best way to do this is to start with code assurance and try to make it a little less painful to catch mistakes <em>before</em> deployment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/ruby/pipe-dream-static-analysis-for-ruby/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>The Brilliance of BDD</title>
		<link>http://www.codecommit.com/blog/java/the-brilliance-of-bdd</link>
		<comments>http://www.codecommit.com/blog/java/the-brilliance-of-bdd#comments</comments>
		<pubDate>Mon, 09 Jun 2008 07:00:59 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/java/the-brilliance-of-bdd</guid>
		<description><![CDATA[As I have previously written, I have recently been spending some time experimenting with various aspects of Scala, including some of the frameworks which have become available.&#160; One of the frameworks I have had the privilege of using is the somewhat unassumingly-titled Specs, and implementation of the behavior-driven development methodology in Scala.
Specs takes full advantage [...]]]></description>
			<content:encoded><![CDATA[<p>As I have <a href="http://www.codecommit.com/blog/scala/naive-text-parsing-in-scala">previously written</a>, I have recently been spending some time experimenting with various aspects of Scala, including some of the frameworks which have become available.&nbsp; One of the frameworks I have had the privilege of using is the somewhat unassumingly-titled <a href="http://code.google.com/p/specs/">Specs</a>, and implementation of the <a href="http://en.wikipedia.org/wiki/Behavior_driven_development">behavior-driven development</a> methodology in Scala.</p>
<p>Specs takes full advantage of Scala&#8217;s flexible syntax, offering a very natural format for structuring tests.&nbsp; For example, we could write a simple specification for a hypothetical <code>add</code> method in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">object</span> AddSpec <span style="color: #140dcc;">extends</span> Specification <span class="br0">&#123;</span>
  <span style="color: #cb0710;">&quot;add method&quot;</span> should <span class="br0">&#123;</span>
    <span style="color: #cb0710;">&quot;handle simple positives&quot;</span> in <span class="br0">&#123;</span>
      <span style="color: #2e7c0f;">add</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span>, <span style="color: #cb0710;">2</span><span class="br0">&#41;</span> mustEqual <span style="color: #cb0710;">3</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span style="color: #cb0710;">&quot;handle simple negatives&quot;</span> in <span class="br0">&#123;</span>
      <span style="color: #2e7c0f;">add</span><span class="br0">&#40;</span><span style="color: #cb0710;">-1</span>, <span style="color: #cb0710;">-2</span><span class="br0">&#41;</span> mustEqual <span style="color: #cb0710;">-3</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span style="color: #cb0710;">&quot;handle mixed signs&quot;</span> in <span class="br0">&#123;</span>
      <span style="color: #2e7c0f;">add</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span>, <span style="color: #cb0710;">-2</span><span class="br0">&#41;</span> mustEqual <span style="color: #cb0710;">-1</span>
      <span style="color: #2e7c0f;">add</span><span class="br0">&#40;</span><span style="color: #cb0710;">-1</span>, <span style="color: #cb0710;">2</span><span class="br0">&#41;</span> mustEqual <span style="color: #cb0710;">1</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>We could go on, of course, but you get the picture.&nbsp; This code will lead to the execution of four separate assertions in three tests (to put things into JUnit terminology).&nbsp; Fundamentally, this isn&#8217;t too much different than a standard series of unit tests, just with a slightly nicer syntax.</p>
<p>Specs defines a domain-specific language for structuring test assertions in a simple and intuitive way.&nbsp; However, this is hardly the only framework for BDD.&nbsp; Perhaps the most well-known such framework is <a href="http://rspec.info/">RSpec</a>, which answers a similar use-case in the Ruby programming language.&nbsp; Our previous specification could be rewritten using RSpec as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">describe AddLib <span style="color: #140dcc;">do</span>
  it <span style="color: #cb0710;">'should handle simple positives'</span> <span style="color: #140dcc;">do</span>
    add<span class="br0">&#40;</span><span style="color: #cb0710;">1</span>, <span style="color: #cb0710;">2</span><span class="br0">&#41;</span>.<span class="me1">should</span> == <span style="color: #cb0710;">3</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  it <span style="color: #cb0710;">'should handle simple negatives'</span> <span style="color: #140dcc;">do</span>
    add<span class="br0">&#40;</span><span style="color: #cb0710;">-1</span>, <span style="color: #cb0710;">-2</span><span class="br0">&#41;</span>.<span class="me1">should</span> == <span style="color: #cb0710;">-3</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  it <span style="color: #cb0710;">'should handle mixed signs'</span> <span style="color: #140dcc;">do</span>
    add<span class="br0">&#40;</span><span style="color: #cb0710;">1</span>, <span style="color: #cb0710;">-2</span><span class="br0">&#41;</span>.<span class="me1">should</span> == <span style="color: #cb0710;">-1</span>
    add<span class="br0">&#40;</span><span style="color: #cb0710;">-1</span>, <span style="color: #cb0710;">2</span><span class="br0">&#41;</span>.<span class="me1">should</span> == <span style="color: #cb0710;">1</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>The end-result is basically the same: the <code>add</code> method will be tested against the given assertions (all four of them) and the results printed in some sort of report form.&nbsp; In this area, RSpec is significantly more mature than Specs, generating very slick HTML reports and nicely formatted console output.&nbsp; This isn&#8217;t really a fundamental weakness of the Specs framework however, just indicative of the fact that RSpec has been around for a <em>lot</em> longer.</p>
<p>These two frameworks are interesting of course, but they are merely implementations of a much larger concept: behavior-driven development.&nbsp; I&#8217;ve never been much of a fan of unit testing.&nbsp; It&#8217;s always seemed to be incredibly dull and a very nearly fruitless waste of effort.&nbsp; As much as I hate it though, I have to bow to the benefits of a self-contained test suite; and so I press on, cursing JUnit every step of the way.</p>
<p>BDD provides a nice alternative to unit testing.&nbsp; At its core, it is not much different in that test groupings and primitive assertions are used to check all aspects of a test unit against predefined data.&nbsp; However, there is something about the &#8220;flow&#8221; of a behavioral spec that is considerably easier to deal with.&nbsp; For some reason, it is far less painful to devise a comprehensive test suite using BDD principles than conventional unit testing.&nbsp; It seems a little far-fetched, but BDD actually makes it easier to write (and more importantly, formulate) exactly the same tests.</p>
<p>It&#8217;s an odd phenomenon, one which can only be caused by the storyboard flow of the code itself.&nbsp; It is very natural to think of distinct requirements for a test unit when each of these requirements are being labeled and entered in a logical sequence.&nbsp; Moreover, the syntax of both Specs and RSpec is such that there is very little boiler-plate required to setup an additional test.&nbsp; Compare the previous BDD specs with the following JUnit4 example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #140dcc;">public</span> <span style="color: #857d1f;">class</span> MathTest <span class="br0">&#123;</span>
    <span style="color: #663f31;">@Test</span>
    <span style="color: #140dcc;">public</span> <span style="color: #857d1f;">void</span> <span style="color: #2e7c0f;">testSimplePositives</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span style="color: #2e7c0f;">assertEquals</span><span class="br0">&#40;</span><span style="color: #cb0710;">3</span>, <span style="color: #2e7c0f;">add</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span>, <span style="color: #cb0710;">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
&nbsp;
    <span style="color: #663f31;">@Test</span>
    <span style="color: #140dcc;">public</span> <span style="color: #857d1f;">void</span> <span style="color: #2e7c0f;">testSimpleNegatives</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span style="color: #2e7c0f;">assertEquals</span><span class="br0">&#40;</span><span style="color: #cb0710;">-3</span>, <span style="color: #2e7c0f;">add</span><span class="br0">&#40;</span><span style="color: #cb0710;">-1</span>, <span style="color: #cb0710;">-2</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
&nbsp;
    <span style="color: #663f31;">@Test</span>
    <span style="color: #140dcc;">public</span> <span style="color: #857d1f;">void</span> <span style="color: #2e7c0f;">testMixedSigns</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span style="color: #2e7c0f;">assertEquals</span><span class="br0">&#40;</span><span style="color: #cb0710;">-1</span>, <span style="color: #2e7c0f;">add</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span>, <span style="color: #cb0710;">-2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
        <span style="color: #2e7c0f;">assertEquals</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span>, <span style="color: #2e7c0f;">add</span><span class="br0">&#40;</span><span style="color: #cb0710;">-1</span>, <span style="color: #cb0710;">2</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>JUnit just requires that much more syntax.&nbsp; It breaks up the logical flow of the tests and (more importantly) the developer train of thought.&nbsp; What can be worse is this syntax bloat makes it very tempting to just group all of the assertions into a single test &#8211; to save typing if nothing else.&nbsp; This is problematic because one assertion may shadow all the others in the case of a failure, preventing them from ever being executed.&nbsp; This can make certain problems much more difficult to isolate.</p>
<p>Logical flow is extremely important to test structure.&nbsp; BDD frameworks provide a very nice syntax for painlessly defining comprehensive test suites.&nbsp; The really wonderful thing about all of this is that BDD is available on the JVM, right now.&nbsp; There&#8217;s nothing stopping you from writing your code in Java as you normally would, then creating your test suite in Scala using Specs rather than JUnit.&nbsp; Alternatively, you could use RSpec on top of JRuby, or <a href="http://groovy.codehaus.org/Using+GSpec+with+Groovy">Gspec</a> with Groovy.&nbsp; All of these are seamless replacements for a test framework like JUnit, and requiring of far less syntactic overhead.</p>
<p>The growing move toward polyglot programming encourages the use of a separate language when it is best suited to a particular task.&nbsp; In this case, several languages are available which offer far more powerful test frameworks than those which can be found in Java.&nbsp; Why not take advantage of them?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/java/the-brilliance-of-bdd/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Buildr Still Not Ready for Prime Time</title>
		<link>http://www.codecommit.com/blog/java/buildr-still-not-ready-for-prime-time</link>
		<comments>http://www.codecommit.com/blog/java/buildr-still-not-ready-for-prime-time#comments</comments>
		<pubDate>Mon, 12 May 2008 07:00:49 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/java/buildr-still-not-ready-for-prime-time</guid>
		<description><![CDATA[As some of you may know, I&#8217;ve been following the Buildr project with a fair degree of interest of late.&#160; Just last week, Assaf announced their first release as an Apache project: Buildr 1.3.0.&#160; Being of the inquisitive bend, I decided that this would be an excellent time to reevaluate its suitability for use in [...]]]></description>
			<content:encoded><![CDATA[<p>As some of you may know, I&#8217;ve been following the <a href="http://incubator.apache.org/buildr/">Buildr project</a> with a fair degree of interest of late.&nbsp; Just last week, <a href="http://www.nabble.com/Buildr-1.3-is-out!-tt17011441.html">Assaf announced</a> their first release as an Apache project: Buildr 1.3.0.&nbsp; Being of the inquisitive bend, I decided that this would be an excellent time to reevaluate its suitability for use in my projects, both commercial and personal.</p>
<p>A while back, I <a href="http://www.codecommit.com/blog/java/in-search-of-a-better-build-system">evaluated Buildr</a> as a potential replacement for Ant and Maven2 and eventually came to the unfortunate conclusion that it just wasn&#8217;t ready.&nbsp; At the time, Buildr was still hampered by a number of annoying limitations (such as being unable to reconfigure the default directory structure).&nbsp; I&#8217;m happy to say that many of these problems have been fixed in the 1.3 release, as well as a large number of feature additions which I hadn&#8217;t thought to request.&nbsp; Despite all that, I&#8217;m still forced to conclude that Buildr simply isn&#8217;t (yet) suitable as my build tool of choice.</p>
<p>Now before you stone me for utter heresy, try to hear me out.&nbsp; I&#8217;m not dissing Buildr in any way, I really <em>want</em> to use it, but I just can&#8217;t justify moving over to it in the face of all of its current issues.&nbsp; What&#8217;s really unfortunate is that all of these issues can be summed up with a single word: <strong>transitivity</strong>.</p>
<p>One of Maven&#8217;s killer features is the ability to resolve the <em>entire</em> transitive dependency graph.&nbsp; Now I&#8217;ll grant that it does this with varying degrees of success, but for the most part it&#8217;s pretty smart about how it fixes up your CLASSPATH.&nbsp; As of 1.3, Buildr claims to have experimental support for transitive dependency resolution, but judging from the problems I encountered in my experimentation, it&#8217;s barely even deserving of mention as an experiment, much less a full-fledged feature.</p>
<p>To understand why transitive dependencies are such a problem, it is of course necessary to first understand the definition of such.&nbsp; In a word (well, several anyway), a transitive dependency is an <em>inherited</em> dependency, an artifact which is not depended upon directly by the project, but rather by one of its dependencies.&nbsp; This definitions carries recursively to dependent parents, grand-parents and so on, thus defining a transitive dependency graph.&nbsp; Consider it this way:</p>
<p align="center"><img height="253" alt="image" src="http://www.codecommit.com/blog/wp-content/uploads/2008/05/image.png" width="497" border="0"></p>
<p>In the diagram, <strong>MyCoolProject</strong> is the artifact we are trying to compile.&nbsp; The only dependencies we have actually specified for this artifact are the <code>DBPool</code> and <code>Databinder</code> artifacts.&nbsp; However, the <code>Databinder</code> artifact has declared that it depends upon both the <code>Wicket</code> and the <code>ActiveObjects</code> artifacts.&nbsp; ActiveObjects doesn&#8217;t depend upon anything, but <code>Wicket</code> has dependencies <code>SLF4J</code> and on the <code>Servlets</code> API.&nbsp; Thus, our original goal, <strong>MyCoolProject</strong>, has <em>transitive</em> dependencies <code>Wicket</code>, <code>SLF4J</code>, <code>Servlets</code> and <code>ActiveObjects</code>.&nbsp; Quite a bit more than we thought we were asking for when we declared our dependency on <code>Databinder</code>.</p>
<p>In general, this sort of transitive resolution is a good thing.&nbsp; It means that instead of specifying six dependencies, we only had to specify two.&nbsp; Furthermore, we observed the DRY principle by not re-specifying dependency information already contained within the various packages.&nbsp; As with any &#8220;good thing&#8221;, there&#8217;s definitely a valid argument regarding its down sides, but on the whole I&#8217;m quite fond of this feature.</p>
<p>In Maven, this sort of thing happens by default, which can lead to some very confusing and subtle CLASSPATH problems (conflicting packages, etc).&nbsp; Buildr takes a slightly different approach in 1.3 by forcing the use of the <code>transitive</code> method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">repositories.<span class="me1">remote</span> &lt;&lt; <span style="color: #cb0710;">'http://www.ibiblio.org/maven2'</span>
&nbsp;
define <span style="color: #cb0710;">'MyCoolProject'</span> <span style="color: #140dcc;">do</span>
  project.<span class="me1">version</span> = <span style="color: #cb0710;">'1.0'</span>
&nbsp;
  compile.<span class="me1">with</span> transitive<span class="br0">&#40;</span><span style="color: #cb0710;">'xmlrpc:xmlrpc-server:jar:3.0'</span><span class="br0">&#41;</span>
&nbsp;
  package <span style="color: #ca9925;">:jar</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>It&#8217;s easy to see why Buildr is raising such a ruckus in the build genre.&nbsp; It&#8217;s syntax is elegance itself, and since it&#8217;s actually a proper scripting language (Ruby), there&#8217;s really nothing you can&#8217;t do with it.&nbsp; Having to remember to specify the default repository is a bit of a pain, but it&#8217;s certainly something I can live with.&nbsp; The real problem with this example is a little less subtle: It doesn&#8217;t work.</p>
<p>If you create a <code>buildfile</code> with the above contents and then run the <code>buildr</code> command, the results will be something like the following:</p>
<pre>Downloading org.apache.xmlrpc:xmlrpc:jar:3.0
rake aborted!
Failed to download org.apache.xmlrpc:xmlrpc:jar:3.0, tried the following repositories:

http://www.ibiblio.org/maven2/

(See full trace by running task with --trace)</pre>
<p>After a fairly significant amount of digging, I managed to discover that this problem is caused by the fact that Buildr attempts to download a corresponding JAR file for <em>every</em> POM it resolves.&nbsp; This seems logical until you consider that many large projects (including Databinder, Wicket, and Hibernate) define POM-only projects which exist for the sole purpose of creating transitive dependencies.&nbsp; They&#8217;re organizational units, designed to allow projects to depend upon one super-artifact, rather than a dozen sub-projects.&nbsp; It&#8217;s a very common practice, and one which Buildr completely fails to handle appropriately.</p>
<p>After some prodding on the Buildr dev mailing-list, Assaf admitted that this is an issue worth looking at and provided a temporary workaround (pending a rework of the functionality in 1.4):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">def</span> pom_only<span class="br0">&#40;</span>a<span class="br0">&#41;</span>
  artifact a <span style="color: #140dcc;">do</span> |task|
    mkpath File.<span class="me1">dirname</span><span class="br0">&#40;</span>task.<span class="me1">to_s</span><span class="br0">&#41;</span>
    Zip::ZipOutputStream.<span class="me1">open</span> task.<span class="me1">to_s</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>This was about the point that I started wondering if maybe Ant/Ivy would be a better bet, at least for the time being.&nbsp; To use this workaround, you must call the <code>pom_only</code> method once for <em>every</em> POM-only project in the dependency graph.&nbsp; Usually, this means you must invoke Buildr repeatedly and find the troublesome artifacts by trial and error.&nbsp; Not exactly a &#8220;just works&#8221; solution.</p>
<p>Pressing forward however, I unearthed a deeper, even more insidious issue: an intermittent failure to generate Eclipse project meta.&nbsp; I&#8217;m not sure if this is due to the POM-only dependencies or just bad juju, but whatever the reason, it&#8217;s annoying.&nbsp; I&#8217;ve raised the issue on the Buildr mailing lists, but so far no response.&nbsp; Basically, what happens is something like this:</p>
<pre>C:\\Users\\Daniel Spiewak\\Desktop\\MyCoolProject&gt; buildr eclipse
(in C:/Users/Daniel Spiewak/Desktop/MyCoolProject, development)
Completed in 0.499s
C:\\Users\\Daniel Spiewak\\Desktop\\MyCoolProject&gt;</pre>
<p>Not exactly the most helpful output.&nbsp; In case you were wondering, this process did not create the Eclipse metadata.&nbsp; It&#8217;s interesting to note that calling <code>buildr idea</code> (to create project metadata for IntelliJ) seems to work just fine.&nbsp; Whatever causes the bug, it seems to be specific to just the Eclipse project generator.</p>
<p>Buildr is a remarkable project.&nbsp; It shows potential to someday become the de-facto build system, possibly even unseating Ant.&nbsp; Unfortunately, that day is not today.&nbsp; There are too many odd wrinkles and unpredictable errors to really call it a &#8220;finished product&#8221;.&nbsp; Hopefully, the Buildr dev team will continue their excellent work, eventually producing a tool worthy of serious consideration.&nbsp; Until then, I guess that I&#8217;m (still) stuck with Ant.</p>
<p><b>Update:</b> It seems that the issues with transitive dependencies <a href="https://issues.apache.org/jira/browse/BUILDR-63">have been resolved</a> in the latest Buildr versions in their SVN.&nbsp; I&#8217;m looking forward to when this all becomes stable and publicly consumable!</p>
<ul>
<li>Download sample <a href="http://www.codecommit.com/blog/misc/buildr-still-not-ready-for-prime-time/buildfile">buildfile</a> </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/java/buildr-still-not-ready-for-prime-time/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>JRuby Interop DSL in Scala</title>
		<link>http://www.codecommit.com/blog/ruby/jruby-interop-dsl-in-scala</link>
		<comments>http://www.codecommit.com/blog/ruby/jruby-interop-dsl-in-scala#comments</comments>
		<pubDate>Mon, 24 Mar 2008 08:00:52 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Scala]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/ruby/jruby-interop-dsl-in-scala</guid>
		<description><![CDATA[JRuby is an amazing bit of programming.&#160; It has managed to rise from its humble beginnings as a hobby project on SourceForge to the most viable third-party Ruby implementation currently available.&#160; As far as I am aware, JRuby is the only Ruby implementation other than MRI which is capable of running an unmodified Rails application.&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>JRuby is an amazing bit of programming.&nbsp; It has managed to rise from its humble beginnings as a hobby project <a href="http://sourceforge.net/projects/jruby">on SourceForge</a> to the most viable third-party Ruby implementation currently available.&nbsp; As far as I am aware, JRuby is the only Ruby implementation other than MRI which is capable of running an unmodified Rails application.&nbsp; But JRuby&#8217;s innovation is not just limited to a rock-solid Ruby interpreter, it also provides tight integration between Java and Ruby.</p>
<p>There&#8217;s a lot of material out there on how to replace Java with Ruby &#8220;glue code&#8221; in your application.&nbsp; The so-called &#8220;<a href="http://memeagora.blogspot.com/2006/12/polyglot-programming.html">polyglot programming</a>&#8221; technique states that we should embrace multiplicity of language in our applications.&nbsp; Java may be very suitable for the core business logic of the application, but for actually driving the frontend UI, we may want to use something more expressive (like Ruby).&nbsp; JRuby provides some powerful constructs which allow access to Java classes from within any Ruby application.&nbsp; For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #800080;">require</span> <span style="color: #cb0710;">'java'</span>
&nbsp;
JFrame = javax.<span class="me1">swing</span>.<span class="me1">JFrame</span>
JButton = javax.<span class="me1">swing</span>.<span class="me1">JButton</span>
JLabel = javax.<span class="me1">swing</span>.<span class="me1">JLabel</span>
&nbsp;
BorderLayout = java.<span class="me1">awt</span>.<span class="me1">BorderLayout</span>
&nbsp;
<span style="color: #140dcc;">class</span> MainWindow &lt; JFrame
  <span style="color: #140dcc;">def</span> initialize
    <span style="color: #857d1f;">super</span> <span style="color: #cb0710;">'My Test Window'</span>
&nbsp;
    setSize<span class="br0">&#40;</span><span style="color: #cb0710;">300</span>, <span style="color: #cb0710;">200</span><span class="br0">&#41;</span>
    setDefaultCloseOperation EXIT_ON_CLOSE
&nbsp;
    label = JLabel.<span class="me1">new</span><span class="br0">&#40;</span><span style="color: #cb0710;">'You pushed the button'</span>, JLabel::CENTER<span class="br0">&#41;</span>
      label.<span class="me1">visible</span> = <span style="color: #857d1f;">false</span>
    add label
&nbsp;
    button = JButton.<span class="me1">new</span> <span style="color: #cb0710;">'Push Me'</span>
    button.<span class="me1">add_action_listener</span> <span style="color: #140dcc;">do</span>
      label.<span class="me1">visible</span> = <span style="color: #857d1f;">true</span>
    <span style="color: #140dcc;">end</span>
    add<span class="br0">&#40;</span>button, BorderLayout::SOUTH<span class="br0">&#41;</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span>
&nbsp;
window = MainWindow.<span class="me1">new</span>
window.<span class="me1">visible</span> = <span style="color: #857d1f;">true</span></pre></td></tr></table></div>

<p align="center"><img height="232" alt="sshot-1" src="http://www.codecommit.com/blog/wp-content/uploads/2008/03/sshot-1.png" width="333" border="0">&nbsp; <img height="232" alt="sshot-2" src="http://www.codecommit.com/blog/wp-content/uploads/2008/03/sshot-2.png" width="333" border="0">&nbsp; </p>
<p>Not a terribly complex example, but it illustrates some of the major advantages of JRuby.&nbsp; Notice how clean and concise this code is.&nbsp; It wouldn&#8217;t have been much longer had I done this using Java, but it would certainly have been less readable.&nbsp; Ruby is absolutely perfect for this sort of use case (driving a UI).</p>
<p>As I said though, there are a myriad of examples showing this sort of thing.&nbsp; As such, it&#8217;s not a very interesting topic for a posting.&nbsp; What the masses have failed to cover, however, is how to accomplish the opposite: calling from Java into Ruby.</p>
<p>Likely the reason this topic has received less attention is because Java is the language will the veritable zoo of libraries and frameworks.&nbsp; The amount of effort and research that has been put into Java simply dwarfs the comparative immaturity of the Ruby offerings.&nbsp; Given the disparity, why would you even want to call into Ruby from Java?&nbsp; This conclusion seems logical until one remembers that almost any application which uses Ruby for the frontend must actually pass flow control to Ruby at some point.&nbsp; This means calling some sort of Ruby code.</p>
<h3>The Java Way</h3>
<p>There is <em>some</em> information <a href="http://wiki.jruby.org/wiki/Java_Integration">available</a> on the JRuby Wiki.&nbsp; The wiki article really should include the caveat that &#8220;some experimentation may be required.&#8221;&nbsp; Sufficient information is available, but it is neither intuitive nor convenient.&nbsp; From Java, the syntax for executing an arbitrary Ruby statement looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5">ScriptEngineManager m = <span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">ScriptEngineManager</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
ScriptEngine rubyEngine = m.<span style="color: #2e7c0f;">getEngineByName</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;jruby&quot;</span><span class="br0">&#41;</span>;
ScriptContext context = engine.<span style="color: #2e7c0f;">getContext</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
&nbsp;
context.<span style="color: #2e7c0f;">setAttribute</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;label&quot;</span>, <span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">Integer</span><span class="br0">&#40;</span><span style="color: #cb0710;">4</span><span class="br0">&#41;</span>, ScriptContext.<span class="me1">ENGINE_SCOPE</span><span class="br0">&#41;</span>;
&nbsp;
<span style="color: #140dcc;">try</span> <span class="br0">&#123;</span>
    rubyEngine.<span style="color: #2e7c0f;">eval</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;puts 2 + $label&quot;</span>, context<span class="br0">&#41;</span>;
<span class="br0">&#125;</span> <span style="color: #140dcc;">catch</span> <span class="br0">&#40;</span>ScriptException e<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    e.<span style="color: #2e7c0f;">printStackTrace</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>It&#8217;s a typical Java API: over-bloated, over-designed and over-generic.&nbsp; What would be really nice is to have a syntax for accessing Ruby objects that is as seamless as accessing Java from Ruby.&nbsp; I want to be able to call Ruby methods and use Ruby classes with the same ease that I can use Java methods and classes.&nbsp; In short, I want an internal DSL for Ruby.</p>
<p>Unfortunately, Java is a bit constrained in this regard.&nbsp; Java&#8217;s syntax is extremely rigid and does not lend itself well to DSL construction.&nbsp; It&#8217;s certainly <a href="http://dschneller.blogspot.com/2007/08/building-xml-groovy-way-in-java.html">possible</a>, but the result is usually less than satisfactory.&nbsp; We could certainly construct an API around the the <a href="https://scripting.dev.java.net">Java Scripting API</a> (JSR-233) which provides more high-level access (such as direct method calls and object wrappers), but it would be clunky and only a marginal improvement over the original.</p>
<p>The good news is there&#8217;s another language tightly integrated with Java that has a far more flexible syntax.&nbsp; Rather than building our JSR-233 wrapper in Java, we can avail ourselves of Scala&#8217;s power and flexibility, hopefully arriving at a DSL which approaches native &#8220;feel&#8221; in its syntax.</p>
<h3>The Scala Way</h3>
<p>Since we&#8217;re attempting to construct a tightly-integrated API for language calls, the most effective route would be to apply techniques <a href="http://www.codecommit.com/blog/ruby/xmlbuilder-a-ruby-dsl-case-study">already discussed</a> in the context of DSL design.&nbsp; As always, we start with the syntax and allow it to drive the implementation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #999999;">// syntax.scala</span>
<span style="color: #140dcc;">import</span> com.<span class="me1">codecommit</span>.<span class="me1">scalaruby</span>._
&nbsp;
<span style="color: #140dcc;">object</span> Main <span style="color: #140dcc;">extends</span> <span style="color: #857d1f;">Application</span> <span style="color: #140dcc;">with</span> JRuby <span class="br0">&#123;</span>
  <span style="color: #2e7c0f;">require</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;test&quot;</span><span class="br0">&#41;</span>
&nbsp;
  <span style="color: #2e7c0f;">associate</span><span class="br0">&#40;</span><span style="color: #ca9925;">'Person</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">Person</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
  <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Received from multiply: &quot;</span> + <span style="color: #ca9925;">'multiply</span><span class="br0">&#40;</span><span style="color: #cb0710;">123</span>, <span style="color: #cb0710;">23</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
  <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Functional test: &quot;</span> + <span style="color: #2e7c0f;">funcTest</span><span class="br0">&#40;</span><span style="color: #ca9925;">'test_string</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
  <span style="color: #140dcc;">val</span> me = <span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">Person</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Daniel Spiewak&quot;</span><span class="br0">&#41;</span>
  <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Name1: &quot;</span> + me.<span class="me1">name</span><span class="br0">&#41;</span>
  <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Name2: &quot;</span> + <span class="br0">&#40;</span>me-&gt;<span style="color: #ca9925;">'name</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
  me.<span class="me1">name</span> = <span style="color: #cb0710;">&quot;Daniel&quot;</span>
  <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;New Name: &quot;</span> + me.<span class="me1">name</span><span class="br0">&#41;</span>
&nbsp;
  <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Person#toString(): &quot;</span> + me<span class="br0">&#41;</span>
&nbsp;
  <span style="color: #140dcc;">val</span> otherPerson = <span style="color: #ca9925;">'create_person</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">asInstanceOf</span><span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">AnyRef</span><span class="br0">&#93;</span></span>
  <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;create_person type: &quot;</span> + otherPerson.<span style="color: #2e7c0f;">getClass</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
  <span style="color: #2e7c0f;">println</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;create_person value: &quot;</span> + otherPerson.<span class="me1">send</span><span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;name&quot;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
  <span style="color: #2e7c0f;">eval</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;puts 'Ruby integration is amazing'&quot;</span><span class="br0">&#41;</span>
&nbsp;
  <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">funcTest</span><span class="br0">&#40;</span>fun:<span class="br0">&#40;</span><span style="color: #857d1f;">Any</span>*<span class="br0">&#41;</span>=&gt;String<span class="br0">&#41;</span> = <span style="color: #2e7c0f;">fun</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#125;</span>
&nbsp;
<span style="color: #140dcc;">class</span> <span style="color: #2e7c0f;">Person</span><span class="br0">&#40;</span>name:<span style="color: #857d1f;">String</span><span class="br0">&#41;</span> <span style="color: #140dcc;">extends</span> <span style="color: #2e7c0f;">RubyClass</span><span class="br0">&#40;</span><span style="color: #ca9925;">'Person</span>, name<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> name = send<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;name&quot;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
  <span style="color: #140dcc;">def</span> name_=<span class="br0">&#40;</span>n:<span style="color: #857d1f;">String</span><span class="br0">&#41;</span> = <span style="color: #ca9925;">'name</span> = n
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>And the associated Ruby code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #999999;"># test.rb</span>
<span style="color: #140dcc;">class</span> Person
  attr_reader <span style="color: #ca9925;">:name</span>
  attr_writer <span style="color: #ca9925;">:name</span>
&nbsp;
  <span style="color: #140dcc;">def</span> initialize<span class="br0">&#40;</span>name<span class="br0">&#41;</span>
    <span style="color: #663f31;">@name</span> = name
  <span style="color: #140dcc;">end</span>
&nbsp;
  <span style="color: #140dcc;">def</span> to_s
    <span style="color: #cb0710;">&quot;Person: {name=#{name}}&quot;</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span>
&nbsp;
<span style="color: #140dcc;">def</span> test_string
  <span style="color: #cb0710;">'Daniel Spiewak'</span>
<span style="color: #140dcc;">end</span>
&nbsp;
<span style="color: #140dcc;">def</span> multiply<span class="br0">&#40;</span>a, b<span class="br0">&#41;</span>
  a * b
<span style="color: #140dcc;">end</span>
&nbsp;
<span style="color: #140dcc;">def</span> create_person
  Person.<span class="me1">new</span> <span style="color: #cb0710;">'Test Person'</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Obviously we&#8217;re going to need some heavy implicit type conversions.&nbsp; The important thing to note is that we don&#8217;t see any residue of the Java Scripting API, it&#8217;s all been encapsulated by our DSL.&nbsp; We&#8217;ve taken an API which is oriented around single-call, low-level invocations and created a high-level wrapper framework which allows method calls, instantiation and even some form of type-checking.</p>
<p>Starting from the top, we see a call which should be familiar to Rubyists, the <code>require</code> statement.&nbsp; In our framework, this method call is just a bit of syntactic sugar around a call to <code>eval(String)</code>.&nbsp; This semantics are basically the same as within Ruby directly, with the exception of how Ruby source files are resolved.&nbsp; Any script file on the CLASSPATH is fair game, in addition to the normal Ruby locations.&nbsp; This allows us to easily embed Ruby scripts within application JARs, libraries and other Java distributables.</p>
<p>Moving down a bit further, we find a somewhat mysterious call to the <a href="http://www.codecommit.com/blog/scala/function-currying-in-scala">curried</a> <code>associate(Symbol)(RubyObject)</code> method.&nbsp; The purpose of this invocation will become more apparent later on.&nbsp; Suffice it to say that this step is necessary to allow Scala class wrappers around existing Ruby classes.</p>
<p>On the next line of interest, we see for the first time how the framework allows for seamless Ruby method invocation.&nbsp; Unlike Ruby, Scala doesn&#8217;t allow us to simply handle calls to non-existent methods.&nbsp; Because of this limitation, we have to be a bit more clever in how we structure the syntax.&nbsp; In this case, we use <a href="http://alblue.blogspot.com/2007/12/scala-introduction-to-scala-case.html">Scala symbols</a> to represent the method.&nbsp; There doesn&#8217;t seem to be a terribly good explanation of symbols in Scala, but there&#8217;s <a href="http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols">plenty of information</a> regarding how they work in Ruby.&nbsp; Since the concepts are virtually identical, techniques are cross-applicable.</p>
<p>The key to the whole &#8220;symbols as methods&#8221; idea is implicit type conversion.&nbsp; The <code>JRuby</code> trait inherits a set of conversions which look something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">implicit</span> <span style="color: #140dcc;">def</span> sym2Method<span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span>sym:<span style="color: #857d1f;">Symbol</span><span class="br0">&#41;</span>:<span class="br0">&#40;</span><span style="color: #857d1f;">Any</span>*<span class="br0">&#41;</span>=&gt;R = send<span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span><span style="color: #2e7c0f;">sym2string</span><span class="br0">&#40;</span>sym<span class="br0">&#41;</span><span class="br0">&#41;</span>
<span style="color: #140dcc;">implicit</span> <span style="color: #140dcc;">def</span> sym2MethodAssign<span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span>sym:<span style="color: #857d1f;">Symbol</span><span class="br0">&#41;</span> = <span style="color: #140dcc;">new</span> SpecialMethodAssign<span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span>sym<span class="br0">&#41;</span>
&nbsp;
private<span style="color: #7f0055;"><span class="br0">&#91;</span>scalaruby<span class="br0">&#93;</span></span> <span style="color: #140dcc;">class</span> SpecialMethodAssign<span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span>sym:<span style="color: #857d1f;">Symbol</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> intern_=<span class="br0">&#40;</span>param:<span style="color: #857d1f;">Any</span><span class="br0">&#41;</span> = <span style="color: #140dcc;">new</span> RubyMethod<span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span><span style="color: #2e7c0f;">str2sym</span><span class="br0">&#40;</span><span style="color: #2e7c0f;">sym2string</span><span class="br0">&#40;</span>sym<span class="br0">&#41;</span> + <span style="color: #cb0710;">&quot;=&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#40;</span>param<span class="br0">&#41;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Though we haven&#8217;t looked at it yet, it is possible to infer the purpose of the <code>send(String)</code> method.&nbsp; It&#8217;s function is to prepare a call to a Ruby method without actually invoking it.&nbsp; This distinction allows us to pass Ruby methods around as method parameters, just like standard Scala methods.&nbsp; The method returned is actually an instance of class <code>RubyMethod[R]</code> (where <code>R</code> is the return type).&nbsp; Scala allows classes to extend structural types like methods, allowing us to redefine the method invocation semantics for wrapped Ruby calls.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">class</span> RubyMethod<span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span>method:<span style="color: #857d1f;">Symbol</span><span class="br0">&#41;</span> <span style="color: #140dcc;">extends</span> <span class="br0">&#40;</span><span class="br0">&#40;</span><span style="color: #857d1f;">Any</span>*<span class="br0">&#41;</span>=&gt;R<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">import</span> JRuby.<span class="me1">engine</span>
&nbsp;
  <span style="color: #140dcc;">override</span> <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">apply</span><span class="br0">&#40;</span>params:<span style="color: #857d1f;">Any</span>*<span class="br0">&#41;</span> = <span style="color: #2e7c0f;">call</span><span class="br0">&#40;</span>params.<span class="me1">toArray</span><span class="br0">&#41;</span>
&nbsp;
  private<span style="color: #7f0055;"><span class="br0">&#91;</span>scalaruby<span class="br0">&#93;</span></span> <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">call</span><span class="br0">&#40;</span>params:Array<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">Any</span><span class="br0">&#93;</span></span><span class="br0">&#41;</span>:R = <span class="br0">&#123;</span>
    <span style="color: #140dcc;">val</span> context = engine.<span style="color: #2e7c0f;">getContext</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span style="color: #140dcc;">val</span> plist = <span style="color: #140dcc;">new</span> Array<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span><span class="br0">&#40;</span>params.<span class="me1">length</span><span class="br0">&#41;</span>
&nbsp;
    <span style="color: #140dcc;">for</span> <span class="br0">&#40;</span>i &lt;- <span style="color: #cb0710;">0</span> until params.<span class="me1">length</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span style="color: #2e7c0f;">plist</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span> = <span style="color: #cb0710;">&quot;res&quot;</span> + i
      context.<span style="color: #2e7c0f;">setAttribute</span><span class="br0">&#40;</span><span style="color: #2e7c0f;">plist</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span>, JRuby.<span style="color: #2e7c0f;">resolveValue</span><span class="br0">&#40;</span><span style="color: #2e7c0f;">params</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="br0">&#41;</span>, ScriptContext.<span class="me1">ENGINE_SCOPE</span><span class="br0">&#41;</span>
      <span style="color: #2e7c0f;">plist</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span> = <span style="color: #cb0710;">&quot;$&quot;</span> + <span style="color: #2e7c0f;">plist</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span>
    <span class="br0">&#125;</span>
&nbsp;
    <span style="color: #2e7c0f;">evaluate</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="br0">&#41;</span> =&gt; <span style="color: #140dcc;">if</span> <span class="br0">&#40;</span>plist.<span class="me1">length</span> &gt; <span style="color: #cb0710;">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
      <span style="color: #2e7c0f;">sym2string</span><span class="br0">&#40;</span>method<span class="br0">&#41;</span> + <span style="color: #cb0710;">&quot;(&quot;</span> + plist.<span class="me1">reduceLeft</span><span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span><span class="br0">&#40;</span>_ + <span style="color: #cb0710;">&quot;, &quot;</span> + _<span class="br0">&#41;</span> + <span style="color: #cb0710;">&quot;)&quot;</span>
    <span class="br0">&#125;</span> <span style="color: #140dcc;">else</span> <span class="br0">&#123;</span>
      <span style="color: #2e7c0f;">sym2string</span><span class="br0">&#40;</span>method<span class="br0">&#41;</span> + <span style="color: #cb0710;">&quot;()&quot;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span style="color: #140dcc;">protected</span> <span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">evaluate</span><span class="br0">&#40;</span>invoke:<span class="br0">&#40;</span><span class="br0">&#41;</span>=&gt;String<span class="br0">&#41;</span>:R = <span class="br0">&#123;</span>
    <span style="color: #140dcc;">val</span> toRun = <span style="color: #2e7c0f;">invoke</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
    Logger.<span style="color: #2e7c0f;">getLogger</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;com.codecommit.scalaruby&quot;</span><span class="br0">&#41;</span>.<span style="color: #2e7c0f;">info</span><span class="br0">&#40;</span>toRun<span class="br0">&#41;</span>
&nbsp;
    JRuby.<span style="color: #2e7c0f;">handleExcept</span><span class="br0">&#40;</span>JRuby.<span class="me1">wrapValue</span><span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span>engine, engine.<span style="color: #2e7c0f;">eval</span><span class="br0">&#40;</span>toRun, engine.<span style="color: #2e7c0f;">getContext</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>The gist of this code is simply to assign every parameter value to an attribute in the Ruby runtime.&nbsp; Attributes of <code>ENGINE_SCOPE</code> (as defined by JSR-233) are represented as global variables within Ruby.&nbsp; These variables are named sequentially starting from zero.&nbsp; (e.g. <code>$res0</code>, <code>$res1</code>, &#8230;)&nbsp; As you can imagine, this technique tends to be a bit of a concurrency killer.&nbsp; To keep things simple, I decided to completely ignore the issues associated with asynchronous execution.&nbsp; It is certainly possible to adapt the framework to function in a multi-threaded environment, but I didn&#8217;t bother to do it.&nbsp; (one of the perks of blogging is a license to extreme laziness)</p>
<p>Once these parameters are assigned, the method call is evaluated within the context of the runtime.&nbsp; This is done by literally generating the corresponding Ruby code (done in the anonymous method) and then wrapping the return value in an instance of <code>RubyObject</code> (if necessary).&nbsp; Note that the <code>send(String)</code> method does not actually kick-start this invocation process at all.&nbsp; Rather, it creates an instance of <code>RubyMethod[R]</code> which corresponds to the method name.&nbsp; This class extends <code>(Any*)=&gt;R</code>, so it may be used in the normal &#8220;method fashion&#8221; &#8211; by appending parentheses which enclose parameters (if any).</p>
<h3>Supporting Cast</h3>
<p>At this point, it&#8217;s worth taking a moment to examine the specifics of the framework class hierarchy.&nbsp; A number of classes exist to wrap around Ruby objects and methods.&nbsp; We&#8217;ve already seen a few of them (<code>RubyMethod[R]</code> and <code>RubyObject</code>), but it&#8217;s worth going into more detail as to their purpose and relation to one another.&nbsp; </p>
<p>Note that these class names often conflict with existing classes in the JRuby implementation.&nbsp; This odd coincidence is precipitated by the fact that the framework seems to deal with a lot of the same concepts as the JRuby runtime (go figure).&nbsp; Rather than obfuscating my class naming to avoid conflict, I just assume that you will either make use of the enhanced Scala import feature (as I have in the implementation), or just avoid using the JRuby internal classes.</p>
<p align="center"><img height="284" alt="image" src="http://www.codecommit.com/blog/wp-content/uploads/2008/03/image7.png" width="583" border="0"> </p>
<ul>
<li><strong>RubyObject &#8211; </strong>The root of the object hierarchy.&nbsp; This abstract class is designed to encapsulate the core functionality of the generic object (roughly: <code>send</code>, <code>-&gt;</code> and <code>eval</code>) as well as containing all of the implicit type conversions.&nbsp; Most of the syntax-defining magic happens here (more on this later).
<li><strong>JRuby &#8211; </strong>This is the primary type interface between the developer and the framework.&nbsp; Classes which wish to make use of Ruby integration must inherit from this trait.&nbsp; This is where the <code>Logger</code> (for executed statements) is initialized and deactivated.&nbsp; Within the corresponding <code>object</code>, all of the backend resources are managed.&nbsp; This is where the actual <code>ScriptEngineManager</code> instance lives, as well as a set of utility methods to handle wrapping and unwrapping of framework-specific objects.
<li><strong>RubyWrapperObject &#8211; </strong>This implementation of <code>RubyObject</code> is designed to wrap around instances which already exist within the Ruby interpreter.&nbsp; For example, if a Ruby method returns an instance of <code>ActiveRecord::Base</code>, it will be represented in Scala by a corresponding instance of <code>RubyWrapperObject</code>.&nbsp; Note that objects which are equivalent in the Ruby interpreter are not guaranteed to be <em>pointer-equivalent.</em>&nbsp; However, the <code>equals(Object)</code> method is well-defined within <code>RubyObject</code>, thus comparisons between <code>RubyObject</code> instances will return sane results.&nbsp; The <code>==</code> method in Scala is defined in terms of <code>equals(Object)</code>, so existing code will behave rationally.
<li><strong>RubyClass -</strong> With the exception of the <code>JRuby</code> trait, this is likely the only class within the framework which the developer will have to reference explicitly.&nbsp; This class allows developer-defined Scala classes to wrap around existing Ruby classes, providing type-safe method calls and even extended functionality.&nbsp; More on this feature later.
<li><strong>RubyMethod &#8211; </strong>We&#8217;ve already seen how this serves as a wrapper around calls to Ruby methods.&nbsp; However, its default implementation assumes that the method is defined in the global namespace.&nbsp; This is impractical for many method calls (such as dispatch on an object).
<li><strong>RubyInstanceMethod &#8211; </strong>This class solves the problem of object dispatch with <code>RubyMethod</code>.&nbsp; All of the core functionality is identical to its superclass with the exception of the generated Ruby code.&nbsp; Instead of just generating a method call passing parameters, this class will generate a method call on a given Ruby object.&nbsp; Thus, this class depends upon <code>RubyWrapperObject</code> which maintains a reference to a corresponding Ruby instance.</li>
</ul>
<h3>Alternative Dispatch</h3>
<p>Not every method call is made on the enclosing scope.&nbsp; Sometimes it is necessary to call a method in an object to which you have a reference.&nbsp; For example, a method may return an instance of a some Ruby class.&nbsp; This instance will be automatically wrapped by a Scala instance of <code>RubyWrappedObject</code>.&nbsp; Since this Scala class doesn&#8217;t actually define any methods which correspond to the Ruby class, it is necessary to once more utilize the &#8220;symbols as methods&#8221; trick in method dispatch.&nbsp; There are two ways to call a method on an object like this: the <code>send[R](String)</code> method (where <code>R</code> is the return type), and the <code>-&gt;</code> (arrow) operator.</p>
<p>Using the arrow operator is a lot like normal method calls, except with symbols instead of method names.&nbsp; Just like dispatch on the enclosing scope, the call is converted into an instance of <code>RubyMethod</code> (actually, an instance of <code>RubyInstanceMethod</code>) which can then be used as a standard Scala method.&nbsp; The difference between using arrow and dispatching on the enclosing scope is the syntax must be a little more contrived.</p>
<p>Parentheses have the second-highest priority of all the Scala operators (the dot operator (.) has the highest).&nbsp; This means that if we simply &#8220;follow our nose&#8221; where the syntax is concerned, we will arrive at an order of invocation which leads to an undesirable result.&nbsp; Consider the following sample:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> obj = <span style="color: #ca9925;">'create_person</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
obj-&gt;<span style="color: #ca9925;">'name</span><span class="br0">&#40;</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p>The first call is a standard dispatch on the enclosing scope.&nbsp; The second call is what is interesting to us.&nbsp; Reading this line naturally (at least to old C/C++ programmers) we would arrive at the following sequence of events:</p>
<ol>
<li>Get a reference to the <code>name</code> method from the instance contained within <code>obj</code>
<li>Invoke the method, passing no parameters</li>
</ol>
<p>Unfortunately, this is not how the compiler sees things.&nbsp; Because parentheses bind tighter than the arrow operator, it actually resolves the expression in the following way:</p>
<ol>
<li>Get a reference to the <code>name</code> method contained within <em>the enclosing scope</em>
<li>Invoke the method, passing no parameters
<li>Invoke the <code>-&gt;</code> method on the instance within <code>obj</code>, passing the result of <code>name</code> as a parameter</li>
</ol>
<p>This is obviously not what we wanted.&nbsp; Unfortunately, there&#8217;s no way to make the arrow operator bind tighter than parentheses.&nbsp; This is a good thing from a language standpoint, but it causes problems for our syntax.</p>
<p>The solution is to enclose any &#8220;arrow dispatch&#8221; statement within parentheses so as to force the order of evaluation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> obj = <span style="color: #ca9925;">'create_person</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
<span class="br0">&#40;</span>obj-&gt;<span style="color: #ca9925;">'name</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p>It looks a bit weird, but it&#8217;s the only way Scala will allow this to work.&nbsp; This call now evaluates properly, calling the <code>name</code> method on the <code>obj</code> instance, passing no parameters.</p>
<p>There&#8217;s actually another problem associated with arrow dispatch in our DSL: Scala already has an implicit meaning for the arrow operator.&nbsp; The following sample should look familiar to those of you who have worked with Scala in other applications:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">val</span> numbers = <span style="color: #2e7c0f;">Map</span><span class="br0">&#40;</span><span style="color: #cb0710;">1</span> -&gt; <span style="color: #cb0710;">&quot;one&quot;</span>, <span style="color: #cb0710;">2</span> -&gt; <span style="color: #cb0710;">&quot;two&quot;</span>, <span style="color: #cb0710;">3</span> -&gt; <span style="color: #cb0710;">&quot;three&quot;</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p>By default, Scala defines the arrow operator as an alternative syntax for defining <a href="http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-6">2-tuples</a>.&nbsp; This is good for most things, but bad for us.&nbsp; What we want is to define a new implicit type conversion which converts <code>Any</code> into a corresponding instance of <code>RubyWrappedObject</code>.&nbsp; This would allow us to satisfy the syntax given above.&nbsp; However, Scala&#8217;s 2-tuple syntax already defines an implicit type conversion for the <code>Any</code> type which deals with the arrow operator.&nbsp; Rather than examining the context to attempt to disambiguate, the Scala compiler simply gives up and prints an error stating that the implicit type conversions are ambiguous.&nbsp; This poses a bit of a problem and nearly killed the arrow operator idea in design.</p>
<p>The solution is actually to override Scala&#8217;s built-in conversion by defining our own conversion <em>with the same name and signature</em> but which provides us with the option of using our own arrow operator definition.&nbsp; The behavior we want is to allow normal use of the arrow operator when dealing with <code>Any -&gt; Any</code>, but convert to <code>RubyWrappedObject</code> and dispatch when dealing with <code>Any -&gt; Symbol</code>.&nbsp; After a little digging through the Scala standard library, I arrived at the following solution (defined in <code>RubyObject</code>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">implicit</span> <span style="color: #140dcc;">def</span> any2ArrowAssoc<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>a:A<span class="br0">&#41;</span> = <span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">SpecialArrowAssoc</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span>
&nbsp;
private<span style="color: #7f0055;"><span class="br0">&#91;</span>scalaruby<span class="br0">&#93;</span></span> <span style="color: #140dcc;">class</span> SpecialArrowAssoc<span style="color: #7f0055;"><span class="br0">&#91;</span>A<span class="br0">&#93;</span></span><span class="br0">&#40;</span>a:A<span class="br0">&#41;</span> <span style="color: #140dcc;">extends</span> <span style="color: #2e7c0f;">ArrowAssoc</span><span class="br0">&#40;</span>a<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> -&gt;<span class="br0">&#40;</span>sym:<span style="color: #857d1f;">Symbol</span><span class="br0">&#41;</span> = <span class="br0">&#40;</span>a <span style="color: #140dcc;">match</span> <span class="br0">&#123;</span>
    <span style="color: #140dcc;">case</span> obj:RubyObject =&gt; obj
    <span style="color: #140dcc;">case</span> other =&gt; <span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">RubyWrapperObject</span><span class="br0">&#40;</span>other<span class="br0">&#41;</span>
  <span class="br0">&#125;</span><span class="br0">&#41;</span>-&gt;sym
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Notice that we extend Scala&#8217;s pre-existing <code>ArrowAssoc[A]</code> class (which handles the special 2-tuple syntax) and then overload the <code>-&gt;</code> method to work differently with symbols.&nbsp; This code now does precisely what we need.&nbsp; By introducing this extra layer of indirection, as well as by overriding Scala&#8217;s existing conversion, we&#8217;re able to support the arrow syntax as shown in the above examples.</p>
<h4>Sending Messages</h4>
<p>There is one final form of dispatch which allows typed return values: <code>send[R](String)</code>.&nbsp; This is actually the method to which all the other dispatch forms delegate (as it is the most general).&nbsp; This method is very similar to the Ruby <code>send</code> method which allows Smalltalk-style message passing on arbitrary objects.&nbsp; The really important thing about this method though is that it will automatically cast the return value from the method to whatever type you specify, allowing you to define type-safe wrappers around existing Ruby methods in Scala:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">def</span> <span style="color: #2e7c0f;">multiply</span><span class="br0">&#40;</span>a:<span style="color: #800080;">Int</span>, b:<span style="color: #800080;">Int</span><span class="br0">&#41;</span> = send<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #800080;">Int</span><span class="br0">&#93;</span></span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;multiply&quot;</span><span class="br0">&#41;</span><span class="br0">&#40;</span>a, b<span class="br0">&#41;</span>
&nbsp;
<span style="color: #140dcc;">val</span> result:<span style="color: #800080;">Int</span> = <span style="color: #2e7c0f;">multiply</span><span class="br0">&#40;</span><span style="color: #cb0710;">123</span>, <span style="color: #cb0710;">23</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p><code>send</code> is effectively defined as a curried function since it takes a method name as a parameter and returns an instance of <code>RubyMethod</code> as a result.&nbsp; This mimics the behavior of dispatch with symbol literals in that you can use <code>send</code> to generate type-safe partially-applied functions for corresponding Ruby methods.</p>
<p>Note that <code>send</code> could just as easily have taken a symbol as a parameter, rather than a string.&nbsp; However, the metaphor throughout the DSL is &#8220;symbols as methods&#8221;, thus string was used to avoid logical conflict.&nbsp; Scala itself was perfectly happy passing symbol literals around in addition to treating them as methods.</p>
<h3>Class Wrapping</h3>
<p>The final bit of code in the example now so far above us serves as a sample of how one might wrap an existing Ruby class within Scala.&nbsp; <code>Person</code> is actually a class defined in Ruby (as you can see from the Ruby sources).&nbsp; It has a read/write attribute, <code>name</code>, as well as an overridden <code>to_s</code> method.&nbsp; <code>RubyObject</code> already contains the logic for handling calls to <code>toString()</code> and proxying them to Ruby&#8217;s <code>to_s</code>, but the <code>name</code> attribute must be handled explicitly in code.</p>
<p>The goal is basically to provide a type-safe wrapper around the <code>Person</code> Ruby class.&nbsp; We could just as easily dispatch on the automatically wrapped instance of <code>RubyWrappedObject</code> using either syntax described above, but an explicit wrapper is a bit nicer.&nbsp; The compiler can check things for us, and we can even add methods to the class (at least, as far as Scala is concerned) in true Ruby &#8220;open class&#8221; style.&nbsp; All that is necessary to accomplish this wrapper is to extend <code>RubyClass</code> and to define the delegating wrapper methods:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #140dcc;">class</span> <span style="color: #2e7c0f;">Person</span><span class="br0">&#40;</span>name:<span style="color: #857d1f;">String</span><span class="br0">&#41;</span> <span style="color: #140dcc;">extends</span> <span style="color: #2e7c0f;">RubyClass</span><span class="br0">&#40;</span><span style="color: #ca9925;">'Person</span>, name<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> name = send<span style="color: #7f0055;"><span class="br0">&#91;</span><span style="color: #857d1f;">String</span><span class="br0">&#93;</span></span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;name&quot;</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
  <span style="color: #140dcc;">def</span> name_=<span class="br0">&#40;</span>n:<span style="color: #857d1f;">String</span><span class="br0">&#41;</span> = <span style="color: #ca9925;">'name</span> = n
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>We specify which Ruby class we are wrapping as the first parameter in the constructor for <code>RubyClass</code>.&nbsp; The parameters which follow are passed directly to the constructor of the corresponding Ruby class.&nbsp; This Ruby constructor is invoked automatically, instantiating the corresponding wrapped Ruby object in the background.&nbsp; Notice that we specify the name of the Ruby class using a symbol.&nbsp; This is the one place in the framework that we break with the &#8220;symbols as methods&#8221; metaphor.&nbsp; The consequence is a nice, clean syntax for Ruby class wrapping.&nbsp; Unfortunately, it also means that wrapping a class within a non-included namespace (e.g. <code>ActiveRecord::Base</code>) can be a little clunky.&nbsp; The only way to do it is to explicitly invoke the <code>Symbol(String)</code> constructor.&nbsp; (this is required because Scala symbols can only contain alpha-numerics and underscores)</p>
<p>Once we have our wrapped class signature, it&#8217;s easy to define the delegate methods.&nbsp; Scala encourages a blurring of field and method, similar to Ruby.&nbsp; As such, it supports a very Ruby-esque syntax for accessor/mutator pairs.&nbsp; This makes the wrapped syntax just a bit nicer.&nbsp; For the accessor, we make a call to the <code>send</code> method, specifying the return type necessary for the wrapper.&nbsp; The mutator allows us to be a bit more creative.</p>
<p>We don&#8217;t really need type-safe return values for a mutator.&nbsp; We would normally just set the return type as <code>Unit</code> and ignore the result.&nbsp; Thus we can once again use the symbol dispatch syntax.&nbsp; Notice that this time we&#8217;re not directly treating a symbol as a method.&nbsp; We&#8217;re apparently <em>assigning</em> a value to the symbol using the <code>=</code> operator (corresponds to the <code>operator=</code> assignment operator in C++).&nbsp; This is possible through a separate implicit type conversion which generates a one-off utility instance:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala">private<span style="color: #7f0055;"><span class="br0">&#91;</span>scalaruby<span class="br0">&#93;</span></span> <span style="color: #140dcc;">class</span> SpecialMethodAssign<span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span>sym:<span style="color: #857d1f;">Symbol</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span style="color: #140dcc;">def</span> intern_=<span class="br0">&#40;</span>param:<span style="color: #857d1f;">Any</span><span class="br0">&#41;</span> = <span style="color: #140dcc;">new</span> RubyMethod<span style="color: #7f0055;"><span class="br0">&#91;</span>R<span class="br0">&#93;</span></span><span class="br0">&#40;</span><span style="color: #2e7c0f;">str2sym</span><span class="br0">&#40;</span><span style="color: #2e7c0f;">sym2string</span><span class="br0">&#40;</span>sym<span class="br0">&#41;</span> + <span style="color: #cb0710;">&quot;=&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#40;</span>param<span class="br0">&#41;</span>
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>As you can see, all this method does is generate a new symbol which includes the &#8216;=&#8217; character and returns the result of dispatching on the corresponding Ruby method.&nbsp; Note that mutators in Ruby are defined as &#8220;<em><mutator>=</em>&#8220;, thus appending &#8220;=&#8221; to the method name is the appropriate behavior.</p>
<h4>Return Value Wrapping</h4>
<p>There&#8217;s actually a slight problem involved in allowing Scala wrappers around existing Ruby types.&nbsp; Well, not so much a problem as an inconsistency.&nbsp; The problem is simply this: if a Ruby method creates an instance of a Ruby class for which there is a Scala wrapper and returns this value through the framework into Scala, one would expect this value would be wrapped into an instance of the Scala wrapper.&nbsp; If you look in the example far above, there is an example of this in the <code>create_person</code> method.&nbsp; The method creates an instance of Ruby class <code>Person</code> and returns it as a result.</p>
<p>Somehow, the framework must identify that there is a corresponding Scala wrapper and then properly create an instance.&nbsp; This actually poses something of a dilemma in two ways.&nbsp; Number one, Scala has no equivalent to Ruby&#8217;s <code>ObjectSpace</code>, so there&#8217;s no way to get a comprehensive list of all classes which have been defined.&nbsp; Even if we could get this list, the corresponding Ruby class is specified in the constructor parameters to <code>RubyClass</code>, so there&#8217;s no way to obtain the information statically from outside the class.&nbsp; Number two, we have to somehow create an instance of the Scala wrapper class <i>without</i> creating a corresponding instance of the wrapped Ruby class (since we already have one).&nbsp; This means we need some sort of override in the <code>RubyClass</code> constructor.</p>
<p>The best solution to all of these problems is to introduce the <code>associate</code> method.&nbsp; The usage is demonstrated at the top of the example where we associate the <code>Person</code> Ruby class with the <code>Person</code> Scala wrapper class.&nbsp; More specifically, we associate the Ruby class with a pass-by-name parameter which defines how to instantiate the Scala class.&nbsp; This is an important distinction as it solves our second problem of instance creation.&nbsp; The framework has no way of knowing what parameters must be passed to the Scala wrapper constructor, so the instantiation itself must be passed:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="scala"><span style="color: #2e7c0f;">associate</span><span class="br0">&#40;</span><span style="color: #ca9925;">'Person</span><span class="br0">&#41;</span><span class="br0">&#40;</span><span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">Person</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p>As I mentioned previously, this is a pass-by-name parameter which means that it will not be immediately evaluated, but rather on-demand somewhere in the body of <code>associate</code>.&nbsp; The <code>associate</code> method actually takes this value and wraps it in an anonymous method which invokes the instantiation each time a value of Ruby type <code>Person</code> must be wrapped.&nbsp; Just prior to invoking the constructor, an override is put in place within the <code>RubyClass</code> singleton object (not shown in the class hierarchy) to prevent the creation of a corresponding Ruby instance.&nbsp; This is what allows the new instance of Scala class <code>Person</code> to correspond with an existing Ruby value.&nbsp; Here again we&#8217;re sacrificing concurrency for a hacky work-around to a complex problem.&nbsp; Any sort of &#8220;proper&#8221; implementation would have to solve this problem in a more elegant way.</p>
<h3>It Never Ends!</h3>
<p>This post, that is.&nbsp; There&#8217;s so much more I could ramble on about (I never even talked about how exceptions are handled), but this entry is already far too long.&nbsp; Hopefully the material presented here only serves to whet your appetite for slicker JRuby-Scala integration and all the benefits it can bring.&nbsp; I&#8217;ve packaged up the framework presented here as a downloadable archive.&nbsp; The package includes the Ruby engine for the Java Scripting API as well as a <code>jar-complete</code> build made from the JRuby SVN.&nbsp; The project <em>may</em> work with JRuby 1.0, but I doubt it.&nbsp; Anyway, JRuby 1.1 is due shortly, so why bother.&nbsp; Remember that this is extremely untested and very experimental.&nbsp; (I did warn you about the concurrency issues, right?)&nbsp; If this is interesting to people, I may do a proper release into an OSS project somewhere.&nbsp; For right now, I just don&#8217;t have the time.&nbsp; <img src='http://www.codecommit.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<p>I hope this entry gives you an idea of what&#8217;s involved in Scala DSL implementation, as well as an idea of where such a technique may be useful in your own projects.&nbsp; After all, what would be better than <em>everyone</em> being able to write their own Rails-killer and define highly fluid APIs!</p>
<ul>
<li>Download <a href="http://www.codecommit.com/blog/misc/unnamed-scala-ruby.zip">unnamed-scala-ruby.zip</a> (requires Java 6 and Scala 2.7.0)</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/ruby/jruby-interop-dsl-in-scala/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>XMLBuilder: A Ruby DSL Case Study</title>
		<link>http://www.codecommit.com/blog/ruby/xmlbuilder-a-ruby-dsl-case-study</link>
		<comments>http://www.codecommit.com/blog/ruby/xmlbuilder-a-ruby-dsl-case-study#comments</comments>
		<pubDate>Mon, 10 Mar 2008 08:00:07 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/ruby/xmlbuilder-a-ruby-dsl-case-study</guid>
		<description><![CDATA[XML is probably the most ubiquitous and most recognizable format in the modern development landscape.&#160; It&#8217;s simple power in representing hierarchically structured data has made it the standard for representing everything from Word documents to databases.&#160; It&#8217;s also one of the most verbose and meta-rich syntaxes known to man.
So in that sense, XML is a [...]]]></description>
			<content:encoded><![CDATA[<p>XML is probably the most ubiquitous and most recognizable format in the modern development landscape.&nbsp; It&#8217;s simple power in representing hierarchically structured data has made it the standard for representing everything from Word documents to databases.&nbsp; It&#8217;s also one of the most verbose and meta-rich syntaxes known to man.</p>
<p>So in that sense, XML is a mixed blessing.&nbsp; Its flexibility and intuitive nature allows developers to store just about any data in a human readable, easy-to-debug manner.&nbsp; Unfortunately, its verboseness often makes <em>generating</em> the actual XML a very frustrating and boring foray into the land of boiler-plate.&nbsp; Various techniques have been developed over the years to smooth this process (e.g. manipulating a DOM tree or reflectively marshalling objects directly to XML), but on the whole, generating XML in code is just as annoying as it has always been.&nbsp; We&#8217;ve all written code like this in the past:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="java5"><span style="color: #140dcc;">public</span> String <span style="color: #2e7c0f;">toXML</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span style="color: #140dcc;">final</span> String INDENT = <span style="color: #cb0710;">&quot;    &quot;</span>;
    StringBuilder back = <span style="color: #140dcc;">new</span> <span style="color: #2e7c0f;">StringBuilder</span><span class="br0">&#40;</span>
            <span style="color: #cb0710;">&quot;&lt;?xml version=<span class="es0">\&quot;</span>1.0<span class="es0">\&quot;</span> encoding=<span class="es0">\&quot;</span>UTF-8<span class="es0">\&quot;</span>?&gt;<span class="es0">\n</span><span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;
&nbsp;
    back.<span style="color: #2e7c0f;">append</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;&lt;person name=<span class="es0">\&quot;</span>&quot;</span><span class="br0">&#41;</span>.<span style="color: #2e7c0f;">append</span><span class="br0">&#40;</span>getName<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span style="color: #2e7c0f;">append</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;<span class="es0">\&quot;</span>&gt;<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;
    <span style="color: #140dcc;">for</span> <span class="br0">&#40;</span>Book book : <span style="color: #2e7c0f;">getBooks</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        back.<span style="color: #2e7c0f;">append</span><span class="br0">&#40;</span>INDENT<span class="br0">&#41;</span>.<span style="color: #2e7c0f;">append</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;&lt;book&gt;&quot;</span><span class="br0">&#41;</span>.<span style="color: #2e7c0f;">append</span><span class="br0">&#40;</span>book.<span style="color: #2e7c0f;">getTitle</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>.<span style="color: #2e7c0f;">append</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;&lt;/book&gt;<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>
    back.<span style="color: #2e7c0f;">append</span><span class="br0">&#40;</span><span style="color: #cb0710;">&quot;&lt;/person&gt;&quot;</span><span class="br0">&#41;</span>;
&nbsp;
    <span style="color: #140dcc;">return</span> back.<span style="color: #2e7c0f;">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span></pre></td></tr></table></div>

<p>Not the most pleasant of algorithms.&nbsp; Oh there&#8217;s nothing complex or challenging about the code, it&#8217;s just annoying (as String manipulation often is).&nbsp; Things would get a little more interesting if we actually had some sort of recursive hierarchy to traverse, but even then it would still be pretty straight-forward.&nbsp; XML generation is tedious grudge-work to which we all must submit from time to time.</p>
<h3>Domain Specific Languages</h3>
<p>There&#8217;s a new wave in programming (especially in the communities surrounding dynamic languages like Ruby and Groovy) called &#8220;Domain Specific Language&#8221;, or DSL for short.&nbsp; The DSL technique has really been around for a long time, but it&#8217;s just now finding its way to the mainstream, &#8220;Citizen Joe&#8221; developers.&nbsp; This is because for decades, domain specific languages have been separate languages unto themselves, with their own syntax, quirks, and utter lack of tool support.</p>
<p>For example, if a company wanted to specify their business logic in a more readable format using a DSL, the would have to spend months, sometimes years of effort to build an entirely new language, just for the one application.&nbsp; While there was no need to generate a truly flexible and extensible syntax (or even one which was Turing-complete), such efforts would still require an enormous amount of work to be put into trivialities like parsers, AST walkers and core libraries.&nbsp; Obviously this meant that DSLs were extremely uncommon.&nbsp; There are very few use-cases for something which requires so much and gains you so little.</p>
<p>This variety of domain specific language is called an &#8220;External DSL&#8221;.&nbsp; This name stems from the fact that the language is completely independent and <em>external</em> to other languages.&nbsp; The innovation which made the DSL attainable for the common-man is that of the &#8220;Internal DSL&#8221;.</p>
<p>People often struggle to precisely define internal DSLs.&nbsp; In the broadest sense, an internal DSL is a language API carefully structured to satisfy a particular syntax when used in code.&nbsp; There is no syntax parsing involved in implementing an internal DSL.&nbsp; Rather, effort is focused on the API itself.&nbsp; The more flexible the syntax of the underlying language, the more powerful and potentially intuitive the syntax of the DSL can be.&nbsp; It is for this reason that languages such as Ruby, Python, Groovy and company are often used to implement internal DSLs.&nbsp; These languages are defined by extremely flexible and dynamic syntax, lending themselves perfectly to such efforts.</p>
<p>With this in mind, it should theoretically be possible to design an &#8220;API&#8221; for XML generation that&#8217;s simple and intuitive.&nbsp; The DSL could be implemented using Ruby, though actually sufficiently dynamic language could do.&nbsp; Implementing an internal DSL is a notoriously difficult task, so perhaps a step-by-step walk through is in order.</p>
<h3>Getting Started</h3>
<p>The very first step in creating an internal DSL is to design the syntax.&nbsp; Similar to how test-driven development starts with a set of unit tests and builds functionality which satisfies the tests, DSL development starts with a few code samples and builds an API to satisfy the syntax.&nbsp; This step will guide all of the code we write as we implement the DSL.</p>
<p>One of the primary goals for our DSL syntax should be to reflect (as much as possible) the structure and essence of the XML output in the generating code.&nbsp; One of the major shortcomings of the ad hoc &#8220;string concat&#8221; XML generation technique is an utter lack of logical structure in the code.&nbsp; Sure your <em>algorithm</em> may be nicely organized and formatted, but does it really reflect the actual structure of the XML it&#8217;s generating?&nbsp; Probably not.&nbsp; Another major goal should be brevity.&nbsp; XML generating code is extremely verbose, and the whole idea behind writing a DSL for XML generation is to elevate some of this hassle.</p>
<p>So, we&#8217;ve got brevity and logical structure.&nbsp; As minor goals, we also may want to spend some effort making the syntax versatile.&nbsp; We don&#8217;t want the algorithm to perform incorrectly if we try to generate a document with the < character in some field.&nbsp; With that said however, we don't need to be <em>overly</em> concerned with flexibility.&nbsp; Yes, our DSL should be as capable as possible, but not to the detriment of brevity and clarity.&nbsp; These concerns are paramount, functionality can take a back seat.</p>
<p>With these goals in mind, let&#8217;s try writing a static syntax for our Person/Book example above. Bear in mind that every construct used in the syntax must be valid Ruby which the interpreter will swallow.&nbsp; <em>How</em> it will swallow is unimportant right now.&nbsp; For this step, it&#8217;s helpful to remember that the <em>ruby -c <filename.rb></em>command will perform a syntax check on a source file without actually attempting to run any sort of interpretation.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">xml.<span class="me1">person</span> <span style="color: #ca9925;">:name</span> =&gt; <span style="color: #cb0710;">'Daniel &quot;Leroy&quot; Spiewak'</span> <span style="color: #140dcc;">do</span>
  book <span style="color: #140dcc;">do</span>
    <span style="color: #cb0710;">'Godel, Escher, Bach'</span>
  <span style="color: #140dcc;">end</span>
  book <span style="color: #140dcc;">do</span>
    <span style="color: #cb0710;">'The Elegant Universe'</span>
  <span style="color: #140dcc;">end</span>
  book <span style="color: #140dcc;">do</span>
    <span style="color: #cb0710;">'Fabric of the Cosmos'</span>
  <span style="color: #140dcc;">end</span>
  book <span style="color: #140dcc;">do</span>
    <span style="color: #cb0710;">&quot;The Hitchhiker's Guide to the Galaxy&quot;</span>
  <span style="color: #140dcc;">end</span>
  book    <span style="color: #999999;"># book with no title</span>
<span style="color: #140dcc;">end</span>
&nbsp;
puts xml  <span style="color: #999999;"># prints the generated XML</span></pre></td></tr></table></div>

<p>Note that this is all static, there&#8217;s no dynamically generated <em>data</em> to muddle the picture.&nbsp; We can worry about that later.</p>
<p>This code sample gives us a rough idea of what the syntax should look like.&nbsp; According to <em>ruby -c</em>, it&#8217;s valid syntax, so we&#8217;re ready to proceed.&nbsp; Everything looks fairly unencumbered by meta-syntax, and the logical structure is certainly represented.&nbsp; Now we do a quick mental pass through the syntax to ensure that all of our <em>functional</em> bases are covered.&nbsp; In the example we&#8217;ve used attributes, nested elements and text data.&nbsp; One of our attributes is making use of illegal XML characters (double quotes).&nbsp; We haven&#8217;t tried mixing elements and text yet, but it&#8217;s fairly obvious how this could be done.</p>
<p>Strictly speaking, we haven&#8217;t rigorously <em>defined</em> anything.&nbsp; What we have done is given ourselves a framework to build off of.&nbsp; This sample will serve as an invaluable template to guide our coding.</p>
<h3>Parsing the Syntax</h3>
<p>Remember I said that internal DSLs do not involve any syntax parsing?&nbsp; Well, in a way I was wrong.&nbsp; The next thing we need to do is walk through the syntax ourselves and understand how Ruby will understand it.&nbsp; This step requires some fairly in-depth knowledge of Ruby&#8217;s (or whatever language you&#8217;re using) syntax.&nbsp; Hopefully the following diagram will help to clarify the process.</p>
<p align="center"><img height="232" alt="image1" src="http://www.codecommit.com/blog/wp-content/uploads/2008/03/image1.png" width="525" border="0"> </p>
<p><p>I&#8217;ve annotated the areas of interest in cute, candy colors to try and illustrate roughly what the Ruby syntax parser will see when it looks at this code.&nbsp; It&#8217;s important to understand this information since it is the key to translating our contrived syntax into a rigorous API.</p>
<p>For the sake of this discussion, I&#8217;m going to assume you have a working knowledge of Ruby and have already recognized some of the more basic syntax elements within the sample.&nbsp; For example, the <em>do/end</em> block is just that, a <em>Block </em>object (roughly the Ruby equivalent of a closure).&nbsp; The annotated String literal is an implicit return value for the inner block (the last statement of a block is implicitly the return value).&nbsp; This syntax employs a Ruby &#8220;trick&#8221; which allows us to drop the <em>return</em> statement (an important trick, since you can&#8217;t return from a block anyway).</p>
<p>Anyone familiar with Ruby on Rails should be aware of Ruby&#8217;s <a href="http://www.codecommit.com/blog/java/java-needs-map-syntax-sugar">convenient Hash literal syntax</a> employed for the element attributes.&nbsp; As annotated, this literal is being passed as a method parameter (Ruby allows us to drop the parentheses).&nbsp; Here again we&#8217;re using little oddities in the Ruby syntax to clean up our DSL and reduce unnecessary cruft.</p>
<p>You should also be familiar with the blurred line between variable and method so common in Ruby.&nbsp; As annotated, the <em>xml</em> syntax element could be either a local field, or a method that we&#8217;re invoking without the parentheses.&nbsp; To clear up this point, we need to refer to the full sample above.&nbsp; In no place do we declare, nor do we provide for the declaration of a local variable <em>xml.</em>&nbsp; Thus, <em>xml()</em> will be a method within our API available in the global scope.</p>
<p>Now we get to the really interesting stuff: those mysterious undefined methods.&nbsp; As with other dynamic languages, Ruby allows method invocations upon non-existent methods.&nbsp; To someone from a static language background, this seems rather peculiar, but it&#8217;s perfectly legal I assure you.&nbsp; In a sense, it&#8217;s much like the &#8220;methods as messages&#8221; metaphor employed by Objective-C and Smalltalk.&nbsp; When you call the method, you&#8217;re just broadcasting a message to the object, hoping someone answers the call.</p>
<p>In Ruby, when a method is called which has no corresponding handler, a special method called <em>method_missing</em> is invoked.&nbsp; This method is what allows us to handle messages even without a pre-defined method body.&nbsp; ActiveRecord makes extensive use of this feature.&nbsp; Every time you access a database field, you&#8217;re calling a non-existent method and thus, <em>method_missing.</em></p>
<p>So based on the first non-existent method we&#8217;re calling (<em>person(&#8230;)</em>), we can already say something about our API.&nbsp; Somewhere in the global scope, we&#8217;re going to need to have a method called <em>xml()</em>.&nbsp; This method will return an instance of a class which defines <em>method_missing</em> and so can handle our <em>person(&#8230;)</em> invocation.&nbsp; It seems this implementation of <em>method_missing</em> will also need to optionally take a <em>Block</em> as the final parameter, allowing it to pass execution on to its logical children.&nbsp; When referring back to our original sample, the final line seems to indicate that the instance must implement the <em>to_s()</em> method, allowing <em>puts()</em> to implicitly convert it to a String.</p>
<h3>Implementation</h3>
<p>All that work and we still haven&#8217;t even written a solid line of code.&nbsp; What we have done though is given ourselves a clear idea of where we need to go, and a rough outline for how it can be done.&nbsp; We&#8217;ve laid the foundation for the implementation by giving ourselves two critical starting points: <em>xml()</em> and that mysterious outer-scoped <em>method_missing.</em>&nbsp; We may not know <em>how</em> we&#8217;re going to implement all this yet, but we at least have an idea on where to begin.</p>
<p>Starting with the easy one, let&#8217;s implement a basic framework around <em>xml()</em>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #800080;">require</span> <span style="color: #cb0710;">'singleton'</span>
&nbsp;
<span style="color: #140dcc;">module</span> XML
  <span style="color: #140dcc;">class</span> XMLBuilder
    attr_reader <span style="color: #ca9925;">:last</span>
&nbsp;
    private
    <span style="color: #140dcc;">def</span> initialize
    <span style="color: #140dcc;">end</span>
&nbsp;
    <span style="color: #140dcc;">def</span> method_missing<span class="br0">&#40;</span>sym, *args, &amp;block<span class="br0">&#41;</span>
      <span style="color: #999999;"># ...</span>
    <span style="color: #140dcc;">end</span>
&nbsp;
    <span style="color: #140dcc;">def</span> to_s
      <span style="color: #663f31;">@last</span>
    <span style="color: #140dcc;">end</span>
&nbsp;
    <span style="color: #140dcc;">def</span> <span style="color: #857d1f;">self</span>.<span class="me1">instance</span>
      <span style="color: #663f31;">@@instance</span> ||= new
    <span style="color: #140dcc;">end</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span>
&nbsp;
<span style="color: #140dcc;">def</span> xml
  XML::XMLBuilder.<span class="me1">instance</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Notice how we&#8217;re using a singleton instance of <em>XMLBuilder</em>?&nbsp; That&#8217;s because there&#8217;s no need for us to have more than one instance exposed to the DSL users.&nbsp; <em>XMLBuilder</em> is just a placeholder class that dispatches the first level of commands for the DSL and will assemble the result for us as it executes.&nbsp; Thus, <em>XMLBuilder</em> can be considered the root of our DSL, the corner-stone upon which the entire API is bootstrapped.&nbsp; We do however need to allow for other, private instances as we&#8217;ll see later on.</p>
<p>Another item worthy of note in this snippet is the non-standard <em>method_missing</em> signature.&nbsp; This is because we will actually need the block as a proper <em>Proc</em> object down the line.&nbsp; A block parameter (prefixed with &amp;) is the only parameter which can follow a varargs parameter (prefixed with *) and there can only be one of them.</p>
<p>We can now try a first-pass implementation of <em>method_missing</em>.&nbsp; This implementation is really just a sample with a very significant shortcoming.&nbsp; The actual implementation is quite a bit more complex.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">def</span> method_missing<span class="br0">&#40;</span>sym, *args, &amp;block<span class="br0">&#41;</span>
  <span style="color: #663f31;">@last</span> = <span style="color: #cb0710;">&quot;&lt;#{sym.to_s}&quot;</span>
&nbsp;
  <span style="color: #140dcc;">if</span> args.<span class="me1">size</span> &gt; <span style="color: #cb0710;">0</span> and args<span class="br0">&#91;</span><span style="color: #cb0710;">0</span><span class="br0">&#93;</span>.<span class="me1">is_a</span>? Hash  <span style="color: #999999;"># never hurts to be sure</span>
    args<span class="br0">&#91;</span><span style="color: #cb0710;">0</span><span class="br0">&#93;</span>.<span class="me1">each</span> <span style="color: #140dcc;">do</span> |key, value|
      <span style="color: #663f31;">@last</span> += <span style="color: #cb0710;">&quot; #{key.to_s}=<span class="es0">\&quot;</span>#{value.to_s}<span class="es0">\&quot;</span>&quot;</span>
    <span style="color: #140dcc;">end</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  <span style="color: #140dcc;">if</span> block.<span style="color: #857d1f;">nil</span>?
    <span style="color: #663f31;">@last</span> += <span style="color: #cb0710;">&quot;/&gt;&quot;</span>   <span style="color: #999999;"># if there's no children, just close the tag</span>
  <span style="color: #140dcc;">else</span>
    <span style="color: #663f31;">@last</span> += <span style="color: #cb0710;">&quot;&gt;&quot;</span>
&nbsp;
    builder = XMLBuilder.<span class="me1">new</span>
    builder.<span class="me1">instance_eval</span> block
&nbsp;
    <span style="color: #663f31;">@last</span> += builder.<span class="me1">last</span>
&nbsp;
    <span style="color: #663f31;">@last</span> += <span style="color: #cb0710;">&quot;&lt;/#{sym.to_s}&gt;&quot;</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>Again, this is just the rough idea.&nbsp; In our actual implementation we need to be concerned about things like valid attribute values (as demonstrated in our sample), proper element names, etc.</p>
<p>The key to the whole algorithm is the <i>instance_eval</i> invocation.&nbsp; This single statement passes control to the next block down and starts the process all over again.&nbsp; The important thing about this is evaluating the block within the context of the an <i>XMLBuilder</i> instance, rather than <i>just</i> its enclosing context.&nbsp; This allows the nested block to take advantage of the same <i>method_missing</i> implementaiton, hence implicitly recursing further into the XML tree.&nbsp; This technique is extremely powerful and absolutely critical to a lot of DSL implementations.</p>
<p>You&#8217;ll also notice that this is breaking the singleton pattern we established in our class design.&nbsp; This is because a separate instance of <i>XMLBuilder</i> is required to handle each nested block within the DSL tree.&nbsp; It&#8217;s very important to remember that we&#8217;re not actually exposing this instance in the public API, it&#8217;s just a tool we use within the implementation.&nbsp; The API users will still only see the singleton <i>XMLBuilder</i> instance.</p>
<p>So a bit more semantically, what we&#8217;re doing is the following:</p>
<ol>
<li>Handle the root non-existant method invocation
<li>Deal with any attributes in the single <i>Hash</i> parameter (if exists)
<li>Check for nested block. If found, create a <i>new</i> builder instance and use its context to evaluate the child block
<li>Within child block evaluation, recurse to step 1 for each method invocation
<li>Accumulate result from child block evaluation and return </li>
</ol>
<p>Of course, this doesn&#8217;t deal with nested text content within elements.&nbsp; However, the principles of the implementation are fairly clear and the rest is just code.</p>
<p>As with all internal DSLs, the user-friendly DSL syntax is supported by an API that&#8217;s ugly, hacky and heavily dependant on language quirks (such as the super-critical <i>instance_eval</i>).&nbsp; In fact, it has been said that internal DSLs <em>encourage</em> the use of language quirks if it simplifies the API from the end-developer standpoint.&nbsp; Of course this makes the end-developer code very clean and easy to maintain at the cost of the DSL developer&#8217;s code, which is nightmarish and horrible to work with.&nbsp; It&#8217;s a tradeoff that must be considered when the decision is made to go with a DSL-style API.</p>
<h3>Conclusion</h3>
<p>Hopefully this was a worthwhile trek into the gory innards of implementing an internal DSL.&nbsp; I leave you with one final code sample to whet your appetite for the fully-implemented API.&nbsp; This is an extract from the <a href="https://activeobjects.dev.java.net">ActiveObjects</a> Ant build file converted to use our new DSL.&nbsp; It&#8217;s interesting that this converted version is significantly cleaner than the original, XML form.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #800080;">require</span> <span style="color: #cb0710;">'xmlbuilder'</span>
&nbsp;
xml.<span class="me1">project</span> <span style="color: #ca9925;">:name</span> =&gt; <span style="color: #cb0710;">'ActiveObjects'</span>, <span style="color: #ca9925;">:default</span> =&gt; <span style="color: #ca9925;">:build</span> <span style="color: #140dcc;">do</span>
  dirname <span style="color: #ca9925;">:property</span> =&gt; <span style="color: #cb0710;">'activeobjects.dir'</span>, <span style="color: #ca9925;">:file</span> =&gt; <span style="color: #cb0710;">'${ant.file.ActiveObjects}'</span>
  property <span style="color: #ca9925;">:file</span> =&gt; <span style="color: #cb0710;">'${activeobjects.dir}/build.properties'</span>
&nbsp;
  target <span style="color: #ca9925;">:name</span> =&gt; <span style="color: #ca9925;">:init</span> <span style="color: #140dcc;">do</span>
    mkdir <span style="color: #ca9925;">:dir</span> =&gt; <span style="color: #cb0710;">'${activeobjects.dir}/bin'</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  target <span style="color: #ca9925;">:name</span> =&gt; <span style="color: #ca9925;">:build</span>, <span style="color: #ca9925;">:depends</span> =&gt; <span style="color: #ca9925;">:init</span> <span style="color: #140dcc;">do</span>
    javac <span style="color: #ca9925;">:srcdir</span> =&gt; <span style="color: #cb0710;">'${activeobjects.dir}/src'</span>, <span style="color: #ca9925;">:source</span> =&gt; <span style="color: #cb0710;">1.5</span>, <span style="color: #ca9925;">:debug</span> =&gt; <span style="color: #857d1f;">true</span>
  <span style="color: #140dcc;">end</span>
&nbsp;
  target <span style="color: #ca9925;">:name</span> =&gt; <span style="color: #ca9925;">:build_test</span>, <span style="color: #ca9925;">:depends</span> =&gt; <span class="br0">&#91;</span><span style="color: #ca9925;">:init</span>, <span style="color: #ca9925;">:build</span><span class="br0">&#93;</span> <span style="color: #140dcc;">do</span>
    property <span style="color: #ca9925;">:name</span> =&gt; <span style="color: #cb0710;">'javadoc.intern.path'</span>, <span style="color: #ca9925;">:value</span> =&gt; <span style="color: #cb0710;">'${activeobjects.dir}/${javadoc.path}'</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span>
&nbsp;
puts xml</pre></td></tr></table></div>

<p>This will print the following XML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="xml"><span class="sc3"><span style="color: #857d1f;">&lt;?xml</span> <span style="color: #857d1f;">version=</span><span style="color: #cb0710;">&quot;1.0&quot;</span> <span style="color: #857d1f;">encoding=</span><span style="color: #cb0710;">&quot;UTF-8&quot;</span><span style="color: #857d1f;">?<span style="color: #857d1f;">&gt;</span></span></span>
&nbsp;
<span class="sc3"><span style="color: #857d1f;">&lt;project</span> <span style="color: #857d1f;">name=</span><span style="color: #cb0710;">&quot;ActiveObjects&quot;</span> <span style="color: #857d1f;">default=</span><span style="color: #cb0710;">&quot;build&quot;</span><span style="color: #857d1f;">&gt;</span></span>
  <span class="sc3"><span style="color: #857d1f;">&lt;dirname</span> <span style="color: #857d1f;">property=</span><span style="color: #cb0710;">&quot;activeobjects.dir&quot;</span> <span style="color: #857d1f;">file=</span><span style="color: #cb0710;">&quot;${ant.file.ActiveObjects}&quot;</span><span style="color: #857d1f;">/&gt;</span></span>
  <span class="sc3"><span style="color: #857d1f;">&lt;property</span> <span style="color: #857d1f;">file=</span><span style="color: #cb0710;">&quot;${activeobjects.dir}/build.properties&quot;</span><span style="color: #857d1f;">&gt;</span></span><span class="sc3"><span style="color: #857d1f;">&lt;/property</span><span style="color: #857d1f;">&gt;</span></span>
  <span class="sc3"><span style="color: #857d1f;">&lt;target</span> <span style="color: #857d1f;">name=</span><span style="color: #cb0710;">&quot;init&quot;</span><span style="color: #857d1f;">&gt;</span></span>
    <span class="sc3"><span style="color: #857d1f;">&lt;mkdir</span> <span style="color: #857d1f;">dir=</span><span style="color: #cb0710;">&quot;${activeobjects.dir}/bin&quot;</span> <span style="color: #857d1f;">/&gt;</span></span>
  <span class="sc3"><span style="color: #857d1f;">&lt;/target</span><span style="color: #857d1f;">&gt;</span></span>
  <span class="sc3"><span style="color: #857d1f;">&lt;target</span> <span style="color: #857d1f;">name=</span><span style="color: #cb0710;">&quot;build&quot;</span> <span style="color: #857d1f;">depends=</span><span style="color: #cb0710;">&quot;init&quot;</span><span style="color: #857d1f;">&gt;</span></span>
    <span class="sc3"><span style="color: #857d1f;">&lt;javac</span> <span style="color: #857d1f;">source=</span><span style="color: #cb0710;">&quot;1.5&quot;</span> <span style="color: #857d1f;">debug=</span><span style="color: #cb0710;">&quot;yes&quot;</span> <span style="color: #857d1f;">srcdir=</span><span style="color: #cb0710;">&quot;${activeobjects.dir}/src&quot;</span><span style="color: #857d1f;">/&gt;</span></span>
  <span class="sc3"><span style="color: #857d1f;">&lt;/target</span><span style="color: #857d1f;">&gt;</span></span>
  <span class="sc3"><span style="color: #857d1f;">&lt;target</span> <span style="color: #857d1f;">name=</span><span style="color: #cb0710;">&quot;build_test&quot;</span> <span style="color: #857d1f;">depends=</span><span style="color: #cb0710;">&quot;init,build&quot;</span><span style="color: #857d1f;">&gt;</span></span>
    <span class="sc3"><span style="color: #857d1f;">&lt;property</span> <span style="color: #857d1f;">name=</span><span style="color: #cb0710;">&quot;javadoc.intern.path&quot;</span> <span style="color: #857d1f;">value=</span><span style="color: #cb0710;">&quot;${activeobjects.dir}/${javadoc.path}&quot;</span><span style="color: #857d1f;">&gt;</span></span><span class="sc3"><span style="color: #857d1f;">&lt;/property</span><span style="color: #857d1f;">&gt;</span></span>
  <span class="sc3"><span style="color: #857d1f;">&lt;/target</span><span style="color: #857d1f;">&gt;</span></span>
<span class="sc3"><span style="color: #857d1f;">&lt;/project</span><span style="color: #857d1f;">&gt;</span></span></pre></td></tr></table></div>

<p>The fully implemented DSL is available in a single Ruby file. Also linked are some examples to provide a more balanced view of the capabilities.</p>
<ul>
<li><a href="http://www.codecommit.com/blog/misc/xmlbuilder/xmlbuilder.rb">xmlbuilder.rb</a> &#8211; <strong>The actual DSL implementation</strong>
<li><a href="http://www.codecommit.com/blog/misc/xmlbuilder/antfile.rb">antfile.rb</a>
<li><a href="http://www.codecommit.com/blog/misc/xmlbuilder/htmlexample.rb">htmlexample.rb</a>
<li><a href="http://www.codecommit.com/blog/misc/xmlbuilder/testfile.rb">testfile.rb</a> </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/ruby/xmlbuilder-a-ruby-dsl-case-study/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Adding Type Checking to Ruby</title>
		<link>http://www.codecommit.com/blog/ruby/adding-type-checking-to-ruby</link>
		<comments>http://www.codecommit.com/blog/ruby/adding-type-checking-to-ruby#comments</comments>
		<pubDate>Wed, 06 Feb 2008 08:00:58 +0000</pubDate>
		<dc:creator>Daniel Spiewak</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.codecommit.com/blog/ruby/adding-type-checking-to-ruby</guid>
		<description><![CDATA[What&#8217;s the first thing you think of when you consider the Ruby Language?&#160; Dynamic types, right?&#160; Ruby is famous (infamous?) for its extremely flexible type system, and as a so-called &#8220;scripting language&#8221;, the core of this mechanism is a lack of type checking.&#160; This feature allows for some very concise expressions and a great deal [...]]]></description>
			<content:encoded><![CDATA[<p>What&#8217;s the first thing you think of when you consider the Ruby Language?&nbsp; Dynamic types, right?&nbsp; Ruby is famous (infamous?) for its extremely flexible type system, and as a so-called &#8220;scripting language&#8221;, the core of this mechanism is a lack of type checking.&nbsp; This feature allows for some very concise expressions and a great deal of flexibility, but sometimes makes your code quite a bit harder to understand.&nbsp; More importantly, it weakens the assurances that a certain method will actually <em>work</em> when passed a given value.</p>
<p>Several different solutions have been proposed to workaround this limitation.&nbsp; The canonical technique involves intensifying tests and increasing test coverage.&nbsp; Ruby has some excellent unit test frameworks (such as RSpec) which serve to ease the pain associated with this approach, but no matter how you slice it, tests are a pain.&nbsp; Having to rely on tests to take the place of type checking in the code assurance process can be extremely frustrating.</p>
<p>Another, less common technique is to simply perform dynamic type checks within the method itself.&nbsp; Like so:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">def</span> create_name<span class="br0">&#40;</span>fname, lname<span class="br0">&#41;</span>
  raise <span style="color: #cb0710;">&quot;fname must be a String&quot;</span> <span style="color: #140dcc;">unless</span> fname.<span class="me1">kind_of</span>? String
  raise <span style="color: #cb0710;">&quot;lname must be a String&quot;</span> <span style="color: #140dcc;">unless</span> lname.<span class="me1">kind_of</span>? String
&nbsp;
  fname + <span style="color: #cb0710;">&quot; &quot;</span> + lname
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>This code explicitly checking the dynamic <em>kind</em> of the parameter values to ensure that they are of type or subtype of <em>String</em>.&nbsp; The issues with this sample should be relatively obvious.</p>
<p>Primarily, it&#8217;s ugly!&nbsp; This sort of repetitious, boiler-plate conditional checking is exactly the sort of thing Ruby tries to avoid.&nbsp; What&#8217;s more is the added bulk of all of these repetitive checks (assuming you perform one check per-parameter per-method) because far more unwieldy than just improving the rspec test coverage.</p>
<p>While manually type checking may be a bad solution syntactically, it&#8217;s on the right track conceptually.&nbsp; What we really want is some sort of assertion that the parameters are of a certain type, but that won&#8217;t overly bloat our existing code.&nbsp; We need some sort of framework that will &#8220;weave in&#8221; (think <a href="http://en.wikipedia.org/wiki/Aspect_oriented_programming">AOP</a>) its type assertions without getting in the way our our algorithms.</p>
<p>Well it turns out that someone&#8217;s <a href="http://people.freebsd.org/~eivind/ruby/types/">already done this</a>.&nbsp; <a href="http://people.freebsd.org/~eivind/">Eivind Eklund</a> kindly pointed me to his type checking framework in a comment on a previous post.&nbsp; The basic idea is to perform the type checking assertions, but to factor the work out into an API encapsulated by an intuitive DSL.&nbsp; So rather than performing all those nasty <em>unless</em> statements as above, we could simply do something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">typesig String, String
<span style="color: #140dcc;">def</span> create_name<span class="br0">&#40;</span>fname, lname<span class="br0">&#41;</span>
  fname + <span style="color: #cb0710;">&quot; &quot;</span> + lname
<span style="color: #140dcc;">end</span></pre></td></tr></table></div>

<p>It&#8217;s really as simple as that.&nbsp; Passing the type values to the <em>typesig</em> method just prior to a method declaration give the cue to the Types framework to perform some extra work on each call that method.&nbsp; Now we have the runtime assurances that the following code will not work (with a very intuitive error message):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby">create_name<span class="br0">&#40;</span><span style="color: #cb0710;">&quot;Daniel&quot;</span>, <span style="color: #cb0710;">123</span><span class="br0">&#41;</span></pre></td></tr></table></div>

<p>Will produce the folling output:</p>
<pre>ArgumentError: Arg 1 is of invalid type (expected String, got Fixnum)</pre>
<p>But the fun doesn&#8217;t stop there.&nbsp; Ruby encourages the &#8220;<a href="http://en.wikipedia.org/wiki/Duck_typing">duck typing</a>&#8221; pattern, where algorithm developers concern themselves not with what the value <strong>is</strong> but rather what it <strong>does</strong>.&nbsp; This means that the type checking really should be done based on what methods are available, not just the raw type.&nbsp; It turns out that the Types framework supports this as well:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"></td><td class="code"><pre class="ruby"><span style="color: #140dcc;">class</span> Company
  <span style="color: #140dcc;">def</span> name
    <span style="color: #cb0710;">&quot;Blue Danube&quot;</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span>
&nbsp;
<span style="color: #140dcc;">class</span> Person
  <span style="color: #140dcc;">def</span> name
    <span style="color: #cb0710;">&quot;Daniel Spiewak&quot;</span>
  <span style="color: #140dcc;">end</span>
<span style="color: #140dcc;">end</span>
&nbsp;
typesig String, Type::Respond<span class="br0">&#40;</span><span style="color: #ca9925;">:name</span><span class="br0">&#41;</span>
<span style="color: #140dcc;">def</span> output<span class="br0">&#40;</span>msg, value<span class="br0">&#41;</span>
  puts msg + <span style="color: #cb0710;">&quot; &quot;</span> + value.<span class="me1">name</span>
<span style="color: #140dcc;">end</span>
&nbsp;
c = Company.<span class="me1">new</span>
p = Person.<span class="me1">new</span>
&nbsp;
output<span class="br0">&#40;</span><span style="color: #cb0710;">&quot;The company name is: &quot;</span>, c<span class="br0">&#41;</span>
output<span class="br0">&#40;</span><span style="color: #cb0710;">&quot;The person is: &quot;</span>, p<span class="br0">&#41;</span>
&nbsp;
output<span class="br0">&#40;</span><span style="color: #cb0710;">&quot;The programmer is: &quot;</span>, <span style="color: #cb0710;">&quot;a genius&quot;</span><span class="br0">&#41;</span>    <span style="color: #999999;"># error</span></pre></td></tr></table></div>

<p>Types can check not only the kind of the object but also to what methods it responds.&nbsp; This is crucial to enabling its adoption into modern Ruby code bases, many of which rely heavily on this &#8220;duck typing&#8221; technique.</p>
<p>You can think of the Types framework just like another layer in your testing architecture.&nbsp; Obviously it&#8217;s not performing any sort of static type checking (since Ruby has no compile phase).&nbsp; All it&#8217;s doing is providing that extra certainty that you&#8217;re never passing something weird from somewhere in your code, something that would break your algorithm.</p>
<p>So what&#8217;s the catch?&nbsp; Well, obviously you need to have the Types framework installed.&nbsp; It&#8217;s not as easy as just typing <em>gem install types</em> either, since the framework actually predates Ruby Gems.&nbsp; You&#8217;ll have to <a href="http://people.freebsd.org/~eivind/ruby/types/">download the framework</a> and then copy around the <em>types.rb</em> file yourself.&nbsp; But this is just deployment semantics.&nbsp; The more interesting issue are the limitations of the code itself.</p>
<p>As far as I can tell, the only restriction on the framework is that it must be used within a proper class, not in the root scope.&nbsp; This means that all of my examples above would have to be enclosed in a class, rather than just copy-pasted into a .rb file and run in place.&nbsp; But other than this one limitation, the framework is incredibly flexible.&nbsp; I really haven&#8217;t shown you the seriously interesting stuff in terms of the API (there are more examples at the top of the <em>types.rb</em> file).&nbsp; In many ways, Types is actually more powerful than any static type checking mechanism could be (yes, I&#8217;m even including Scala in that evaluation).</p>
<p>I haven&#8217;t had a chance to use Types on any serious project myself, but I can see tremendous potential, particularly for companies with large-scale Ruby/Rails deployments or even smaller projects looking for just a bit tighter code assurance.&nbsp; As far as I&#8217;m concerned, there shouldn&#8217;t be a non-trivial Ruby project attempted without this lovely library, Rails or no Rails.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codecommit.com/blog/ruby/adding-type-checking-to-ruby/feed</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
	</channel>
</rss>

