<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Magnus Holm</title>
  <subtitle>Thoughts on life, internet and programming</subtitle>
  <id>tag:judofyr.net,1992-10-15:/</id>
  <link rel="self" type="application/atom+xml" href="http://judofyr.net/feed/atom.xml"/>
  <link rel="alternate" type="text/html" href="http://judofyr.net/"/>
  <updated>2010-01-18T17:53:36+01:00</updated>
  <author>
    <name>Magnus Holm</name>
    <email>judofyr@gmail.com</email>
    <uri>http://judofyr.net</uri>
  </author>

  <entry>
    <title>Continue that exception</title>
    <id>tag:judofyr.net,2010-01-18:1263833010</id>
    <link href="http://judofyr.net/posts/never-gonna-let-you-go.html"/>
    <updated>2010-01-18T16:43:30Z</updated>
    <published>2010-01-18T16:43:30Z</published>
    <content type="html"><![CDATA[<p>Imagine a world with Exception#continue:</p>
<pre class="sunburst">
<span class="Constant">10</span>.<span class="Entity">times</span> <span class="Keyword">do </span>|<span class="Variable">i</span>|
  <span class="Keyword">begin</span>
    <span class="Keyword">raise</span> <span class="String"><span class="String">&quot;</span>OH NO!<span class="String">&quot;</span></span>
    puts <span class="String"><span class="String">&quot;</span>OH YES! <span class="StringEmbeddedSource"><span class="StringEmbeddedSource">#{</span>i<span class="StringEmbeddedSource">}</span></span><span class="String">&quot;</span></span>
    i <span class="Keyword">+=</span> <span class="Constant">1</span>
  <span class="Keyword">rescue</span> =&gt; err
    <span class="Keyword">if</span> i <span class="Keyword">&lt;</span> <span class="Constant">5</span>
      err.<span class="Entity">continue</span>
    <span class="Keyword">else</span>
      <span class="Keyword">raise</span> err
    <span class="Keyword">end</span>
  <span class="Keyword">end</span>
<span class="Keyword">end</span>
</pre>
<p>Oh, that&#8217;s right. This is Ruby. Forty lines later:</p>
<pre class="sunburst">
<span class="Keyword">class</span> <span class="JEntityNameType">Exception</span>
  <span class="Keyword">class</span> <span class="JEntityNameType">NoContinuation<span class="EntityInheritedClass"> <span class="EntityInheritedClass">&lt;</span> StandardError</span></span>
  <span class="Keyword">end</span>
  
  <span class="Keyword">attr_accessor</span> <span class="Constant"><span class="Constant">:</span>continuation</span>
  
  <span class="Keyword">def</span> <span class="Entity">continue</span>
    <span class="Keyword">raise</span> <span class="Variable">NoContinuation</span> <span class="Keyword">unless</span> continuation.<span class="Entity">respond_to?</span>(<span class="Constant"><span class="Constant">:</span>call</span>)
    continuation.<span class="Entity">call</span>
  <span class="Keyword">end</span>
<span class="Keyword">end</span>

<span class="Keyword">module</span> <span class="JEntityNameType">NeverGonnaLetYouDown</span>
  <span class="Keyword">def</span> <span class="Entity">raise</span>(<span class="Variable">exception <span class="Keyword">=</span> <span class="Variable">RuntimeError</span><span class="Variable">,</span> string <span class="Keyword">=</span> <span class="Constant">nil</span><span class="Variable">,</span> array <span class="Keyword">=</span> caller</span>)
<span class="Comment">    <span class="Comment">#</span> With a single String argument, raises a</span>
<span class="Comment">    <span class="Comment">#</span> RuntimeError with the string as a message. </span>
    <span class="Keyword">if</span> exception.<span class="Entity">is_a?</span>(<span class="Variable">String</span>)
      string <span class="Keyword">=</span> exception
      exception <span class="Keyword">=</span> <span class="Variable">RuntimeError</span>
    <span class="Keyword">end</span>
    
    callcc <span class="Keyword">do </span>|<span class="Variable">cc</span>|
      obj <span class="Keyword">=</span> exception.<span class="Entity">exception</span>(string)
      obj.<span class="Entity">set_backtrace</span>(array)
      obj.<span class="Entity">continuation</span> <span class="Keyword">=</span> cc
      <span class="Keyword">super</span> obj
    <span class="Keyword">end</span>
  <span class="Keyword">end</span>
  
  <span class="Keyword">def</span> <span class="Entity">fail</span>(<span class="Variable">exception <span class="Keyword">=</span> <span class="Variable">RuntimeError</span><span class="Variable">,</span> string <span class="Keyword">=</span> <span class="Constant">nil</span><span class="Variable">,</span> array <span class="Keyword">=</span> caller</span>)
    <span class="Keyword">raise</span>(exception, string, array)
  <span class="Keyword">end</span>
<span class="Keyword">end</span>

<span class="Keyword">class</span> <span class="JEntityNameType">Object</span>
  <span class="Keyword">include</span> <span class="Variable">NeverGonnaLetYouDown</span>
<span class="Keyword">end</span>
</pre>]]></content>
  </entry>

  <entry>
    <title>Temple</title>
    <id>tag:judofyr.net,2009-12-12:1260643090</id>
    <link href="http://judofyr.net/posts/temple.html"/>
    <updated>2009-12-12T18:38:10Z</updated>
    <published>2009-12-12T18:38:10Z</published>
    <content type="html"><![CDATA[<p>I have a theory:</p>
<blockquote>
<p>Every compilable template can be compiled to ERB.</p>
</blockquote>
<p>Well, not ERB-the-syntax, but ERB-the-concept:</p>
<blockquote>
<p>Every compilable template consists of three elements:</p>
<ul>
	<li>Static text</li>
	<li>Dynamic text (pieces of Ruby which are evaluated and sent to the client)</li>
	<li>Blocks (pieces of Ruby which are evaluated and <em>not</em> sent to the client, but might change the control flow).
</blockquote></li>
</ul>
<p>Nothing revolutionary at all. Just a theory.</p>
<h3>Compiling it</h3>
<p>There are several ways to render such a template, but the fastest one is to compile it to pure Ruby:</p>
<pre>
Hello &lt;%= @world %&gt;!
&lt;% if @me.happy? %&gt;
  &lt;%= @greeting %&gt;
&lt;% end %&gt;
</pre>
<p>Becomes:</p>
<pre class="sunburst">
_buf <span class="Keyword">=</span> []
_buf <span class="Keyword">&lt;&lt;</span> <span class="String"><span class="String">&quot;</span>Hello <span class="String">&quot;</span></span>
_buf <span class="Keyword">&lt;&lt;</span> (<span class="Variable"><span class="Variable">@</span>world</span>)
_buf <span class="Keyword">&lt;&lt;</span> <span class="String"><span class="String">&quot;</span>!<span class="StringConstant">\n</span><span class="String">&quot;</span></span>
<span class="Keyword">if</span> <span class="Variable"><span class="Variable">@</span>me</span>.<span class="Entity">happy?</span>
_buf <span class="Keyword">&lt;&lt;</span> <span class="String"><span class="String">&quot;</span><span class="StringConstant">\n</span>  <span class="String">&quot;</span></span>
_buf <span class="Keyword">&lt;&lt;</span> (<span class="Variable"><span class="Variable">@</span>greeting</span>)
_buf <span class="Keyword">&lt;&lt;</span> <span class="String"><span class="String">&quot;</span><span class="StringConstant">\n</span><span class="String">&quot;</span></span>
<span class="Keyword">end</span>
_buf.<span class="Entity">join</span>
</pre>
<p>Probably you also want to optimize it:</p>
<pre class="sunburst">
_buf <span class="Keyword">=</span> []
_buf <span class="Keyword">&lt;&lt;</span> (<span class="String"><span class="String">&quot;</span>Hello <span class="StringEmbeddedSource"><span class="StringEmbeddedSource">#{</span><span class="StringVariable"><span class="StringVariable">@</span>world</span><span class="StringEmbeddedSource">}</span></span>!<span class="StringConstant">\n</span><span class="String">&quot;</span></span>)
<span class="Keyword">if</span> <span class="Variable"><span class="Variable">@</span>me</span>.<span class="Entity">happy?</span>
_buf <span class="Keyword">&lt;&lt;</span> (<span class="String"><span class="String">&quot;</span><span class="StringConstant">\n</span>  <span class="StringEmbeddedSource"><span class="StringEmbeddedSource">#{</span><span class="StringVariable"><span class="StringVariable">@</span>greeting</span><span class="StringEmbeddedSource">}</span></span><span class="StringConstant">\n</span><span class="String">&quot;</span></span>)
<span class="Keyword">end</span>
_buf.<span class="Entity">join</span>
</pre>
<p>Or maybe you rather want <code>_buf</code> to be a String? Or print it directly to stdout?</p>
<p>These options are already written and implemented in Erubis, but none of the other template engines can take advantage of that. Which is a shame, because we know that every compilable template can be compiled to ERB. So <em>technically</em> it shouldn&#8217;t be a problem to share such code.</p>
<h3>Abstractions</h3>
<p>The problem with today&#8217;s template engines is that they aren&#8217;t using enough abstractions. Mustache parses and compiles at the same time<sup class="footnote"><a href="#fn1">1</a></sup>. Haml does parsing/compiling/optimization in some tightly coupled modules. Parkaby separates parsing and compiling, but could share a lot of code with Haml since they&#8217;re both compiling HTML tags. While I haven&#8217;t checked it out, I assume the same applies to <a href="http://github.com/stonean/ruhl">RuHL</a>. Liquid doesn&#8217;t really compile (to Ruby) at all, but rather uses a VM approach.</p>
<p>Every template developers faces the same questions: Should I use an Array or String as buffer? How should I escape it? How can I optimize it? Or, they don&#8217;t even compile it at all.</p>
<h3>Enter the Temple</h3>
<p><a href="http://github.com/judofyr/temple">Temple</a> attempts to solve these problems. Your goal as a template developer is to end up with an Array like this:</p>
<pre class="sunburst">
[<span class="Constant"><span class="Constant">:</span>multi</span>,
 [<span class="Constant"><span class="Constant">:</span>static</span>, <span class="String"><span class="String">&quot;</span>Hello <span class="String">&quot;</span></span>],
 [<span class="Constant"><span class="Constant">:</span>dynamic</span>, <span class="String"><span class="String">&quot;</span>@world<span class="String">&quot;</span></span>],
 [<span class="Constant"><span class="Constant">:</span>static</span>, <span class="String"><span class="String">&quot;</span>!<span class="StringConstant">\n</span><span class="String">&quot;</span></span>],
 [<span class="Constant"><span class="Constant">:</span>block</span>, <span class="String"><span class="String">&quot;</span>if @me.happy?<span class="String">&quot;</span></span>],
 [<span class="Constant"><span class="Constant">:</span>static</span>, <span class="String"><span class="String">&quot;</span><span class="StringConstant">\n</span>  <span class="String">&quot;</span></span>],
 [<span class="Constant"><span class="Constant">:</span>dynamic</span>, <span class="String"><span class="String">&quot;</span>@greeting<span class="String">&quot;</span></span>],
 [<span class="Constant"><span class="Constant">:</span>static</span>, <span class="String"><span class="String">&quot;</span><span class="StringConstant">\n</span><span class="String">&quot;</span></span>],
 [<span class="Constant"><span class="Constant">:</span>block</span>, <span class="String"><span class="String">&quot;</span>end<span class="String">&quot;</span></span>]]
</pre>
<p>Then let Temple take over. You could use Temple::Filters::DynamicInliner to optimize sequential statics/dynamics into a single dynamic. Temple::Filters::Escapable handles escaping, and Temple::Core::ArrayBuffer generates the Ruby code.</p>
<p>The idea is to build an engine based on a chain of compilers. A compiler is simply a class which has a method called <em>#compile</em> which takes one arguments. It&#8217;s illegal for a compiler to mutate the argument, and it should be possible to use the same instance several times.</p>
<p>Very much like you have middlewares in Rack, you use compilers in Temple (except <em>everything</em> is a compiler in Temple):</p>
<pre class="sunburst">
<span class="Keyword">class</span> <span class="JEntityNameType">ERBEngine<span class="EntityInheritedClass"> <span class="EntityInheritedClass">&lt;</span> Temple::Engine</span></span>
  use <span class="Support">Temple</span>::<span class="Entity">Parsers</span>::<span class="Entity">ERB</span>
  use <span class="Support">Temple</span>::<span class="Entity">Filters</span>::<span class="Entity">DynamicInliner</span>
  use <span class="Support">Temple</span>::<span class="Entity">Core</span>::<span class="Entity">ArrayBuffer</span>
<span class="Keyword">end</span>  
</pre>
<h3>Step 1: The parser</h3>
<p>In Temple, a parser is also a compiler, because a compiler is just something that takes some input and produces some output. A parser is then something that takes a String and returns an Array.</p>
<p>A dead simple ERB parser could look like this:</p>
<pre class="sunburst">
<span class="Keyword">class</span> <span class="JEntityNameType">ERB</span>
  <span class="Keyword">def</span> <span class="Entity">compile</span>(<span class="Variable">src</span>)
    result <span class="Keyword">=</span> [<span class="Constant"><span class="Constant">:</span>multi</span>]
    <span class="Keyword">while</span> src <span class="Keyword">=~</span> <span class="StringRegexp"><span class="StringRegexp">/</span></span><span class="StringRegexp">&lt;%<span class="StringRegexp"><span class="StringRegexp">(</span>.*?<span class="StringRegexp">)</span></span>%&gt;</span><span class="StringRegexp"><span class="StringRegexp">/</span></span>
      result <span class="Keyword">&lt;&lt;</span> [<span class="Constant"><span class="Constant">:</span>static</span>, <span class="Variable"><span class="Variable">$</span>`</span>]
      <span class="Keyword">case</span> <span class="Variable"><span class="Variable">$</span>1</span>[<span class="Constant">0</span>]
      <span class="Keyword">when</span> <span class="Constant">?#</span>
        <span class="Keyword">next</span>
      <span class="Keyword">when</span> <span class="Constant">?=</span>
        text <span class="Keyword">=</span> <span class="Variable"><span class="Variable">$</span>1</span>[<span class="Constant">1</span>..<span class="Keyword">-</span><span class="Constant">1</span>].<span class="Entity">strip</span>
        head <span class="Keyword">=</span> <span class="Constant"><span class="Constant">:</span>dynamic</span>
      <span class="Keyword">else</span>
        text <span class="Keyword">=</span> <span class="Variable"><span class="Variable">$</span>1</span>
        head <span class="Keyword">=</span> <span class="Constant"><span class="Constant">:</span>block</span>
      <span class="Keyword">end</span>
      result <span class="Keyword">&lt;&lt;</span> [head, text]
      src <span class="Keyword">=</span> <span class="Variable"><span class="Variable">$</span>'</span>
    <span class="Keyword">end</span>
    result <span class="Keyword">&lt;&lt;</span> [<span class="Constant"><span class="Constant">:</span>static</span>, src]
    result
  <span class="Keyword">end</span>
<span class="Keyword">end</span>
</pre>
<p>It&#8217;s important to remember that the <em>parser should be dumb.</em> No optimization, no guesses. It should produce an Array that is as close to the source as possible. That means you&#8217;ll probably have to invent your own abstraction, but <em>that is exactly the point!</em></p>
<p>The Mustache parser compiles to an Array like this:</p>
<pre class="sunburst">
[<span class="Constant"><span class="Constant">:</span>multi</span>,
  [<span class="Constant"><span class="Constant">:</span>static</span>, <span class="String"><span class="String">&quot;</span>Hello <span class="String">&quot;</span></span>],
  [<span class="Constant"><span class="Constant">:</span>mustache</span>, <span class="Constant"><span class="Constant">:</span>evar</span>, <span class="Constant"><span class="Constant">:</span>world</span>],
  [<span class="Constant"><span class="Constant">:</span>mustache</span>, <span class="Constant"><span class="Constant">:</span>section</span>, <span class="Constant"><span class="Constant">:</span>happy?</span>, 
    [<span class="Constant"><span class="Constant">:</span>mustache</span>, <span class="Constant"><span class="Constant">:</span>evar</span>, <span class="Constant"><span class="Constant">:</span>greeting</span>]]]
</pre>
<p>And that&#8217;s fine. The parser should not care about how to compile this further down. That&#8217;s the job to a <em>filter</em>.</p>
<h3>Step N: Filters</h3>
<p>A filter is a compiler which takes an Array and returns an Array. It might turn convert it one step closer to the core-abstraction, it might create a new abstraction, or it might just optimize in the current abstraction. Ultimately, it&#8217;s still just a compiler which takes an Array and returns an Array.</p>
<p>Temple::<em>Filters</em>::Mustache takes an Array in the Mustache-abstraction and compiles it down to core. Then it has to be ran through Temple::Filters::Escapable which handles HTML escaping.</p>
<p>You might wonder <em>why</em> we split Mustache into a parser and a filter, and there are several reasons. Basically it&#8217;s because the parser shouldn&#8217;t need to worry about the code it should generate. Now it&#8217;s possible to benchmark, rewrite, test and improve the parser, and the parser only.</p>
<p>It&#8217;s also because there isn&#8217;t one definite way to go from a Mustache-string to the core-abstraction, but there is only one way to go from a Mustache-string to the Mustache-abstraction. If we want to experiment with another way of ending up at core, we can now write it without duplicating the parsing code.</p>
<p>Anyway, after you&#8217;ve run it through a few filters, you probably want to generate some Ruby code, and that&#8217;s what a generator does.</p>
<h3>Step N+1: The generator</h3>
<p>A generator is a compiler which takes an Array and returns a String. Generators, just like parsers, are dumb too. Here&#8217;s the ArrayBuffer:</p>
<pre class="sunburst">
<span class="Keyword">class</span> <span class="JEntityNameType">ArrayBuffer<span class="EntityInheritedClass"> <span class="EntityInheritedClass">&lt;</span> Generator</span></span>
  <span class="Keyword">def</span> <span class="Entity">buffer</span>(<span class="Variable">str <span class="Keyword">=</span> <span class="String"><span class="String">'</span><span class="String">'</span></span></span>)
    <span class="String"><span class="String">'</span>_buf<span class="String">'</span></span> <span class="Keyword">+</span> str
  <span class="Keyword">end</span>
  
  <span class="Keyword">def</span> <span class="Entity">preamble</span>;  buffer <span class="String"><span class="String">&quot;</span> = []<span class="StringConstant">\n</span><span class="String">&quot;</span></span> <span class="Keyword">end</span>
  <span class="Keyword">def</span> <span class="Entity">postamble</span>; buffer <span class="String"><span class="String">&quot;</span>.join<span class="String">&quot;</span></span>   <span class="Keyword">end</span>
  
  <span class="Keyword">def</span> <span class="Entity">on_static</span>(<span class="Variable">text</span>)
    buffer <span class="String"><span class="String">&quot;</span> &lt;&lt; <span class="StringEmbeddedSource"><span class="StringEmbeddedSource">#{</span>text<span class="StringEmbeddedSource"><span class="StringEmbeddedSource">.</span><span class="Entity">inspect</span></span><span class="StringEmbeddedSource">}</span></span><span class="StringConstant">\n</span><span class="String">&quot;</span></span>
  <span class="Keyword">end</span>
  
  <span class="Keyword">def</span> <span class="Entity">on_dynamic</span>(<span class="Variable">code</span>)
    buffer <span class="String"><span class="String">&quot;</span> &lt;&lt; (<span class="StringEmbeddedSource"><span class="StringEmbeddedSource">#{</span>code<span class="StringEmbeddedSource">}</span></span>)<span class="StringConstant">\n</span><span class="String">&quot;</span></span>
  <span class="Keyword">end</span>
  
  <span class="Keyword">def</span> <span class="Entity">on_block</span>(<span class="Variable">code</span>)
    code <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span><span class="StringConstant">\n</span><span class="String">&quot;</span></span>
  <span class="Keyword">end</span>
<span class="Keyword">end</span>
</pre>
<p>Temple includes Array, StringBuffer and Interpolation too, even though the last one works kinda funky with blocks and currently only works as expected with Mustache.</p>
<h3>Engines</h3>
<p>The user however would only see an engine, which is a chain of compilers:</p>
<pre class="sunburst">
<span class="Keyword">class</span> <span class="JEntityNameType">ERBEngine<span class="EntityInheritedClass"> <span class="EntityInheritedClass">&lt;</span> Temple::Engine</span></span>
<span class="Comment">  <span class="Comment">#</span> Here using some helpers, but it's important to remember that</span>
<span class="Comment">  <span class="Comment">#</span> it's only sugar around the #use method shown above.</span>
  parser <span class="Constant"><span class="Constant">:</span>ERB</span>
  filter <span class="Constant"><span class="Constant">:</span>DynamicInliner</span>
  generator <span class="Constant"><span class="Constant">:</span>ArrayBuffer</span>
<span class="Keyword">end</span>  
</pre>
<p>The core of Temple::Engine is simple. So simple I&#8217;d like to show it to you (without the helpers):</p>
<pre class="sunburst">
<span class="Keyword">class</span> <span class="JEntityNameType">Engine</span>
  <span class="Keyword">def</span> <span class="Entity">self.filters</span>
    <span class="Variable"><span class="Variable">@</span>filters</span> <span class="Keyword">||=</span> []
  <span class="Keyword">end</span>
  
  <span class="Keyword">def</span> <span class="Entity">self.use</span>(<span class="Variable">filter<span class="Variable">,</span> <span class="Keyword">*</span>args<span class="Variable">,</span> <span class="Keyword">&amp;</span>blk</span>)
    filters <span class="Keyword">&lt;&lt;</span> [filter, args, blk]
  <span class="Keyword">end</span>
  
  <span class="Keyword">def</span> <span class="Entity">initialize</span>
    <span class="Variable"><span class="Variable">@</span>chain</span> <span class="Keyword">=</span> <span class="Variable">self</span>.<span class="Entity">class</span>.<span class="Entity">filters</span>.<span class="Entity">map</span> <span class="Keyword">do </span>|<span class="Variable">filter</span>, <span class="Variable">args</span>, <span class="Variable">blk</span>|
      filter.<span class="Entity">new</span>(<span class="Keyword">*</span>args, <span class="Keyword">&amp;</span>blk)
    <span class="Keyword">end</span>
  <span class="Keyword">end</span>
  
  <span class="Keyword">def</span> <span class="Entity">compile</span>(<span class="Variable">thing</span>)
    <span class="Variable"><span class="Variable">@</span>chain</span>.<span class="Entity">inject</span>(thing) { |<span class="Variable">prev_thing</span>, <span class="Variable">compiler</span>| compiler.<span class="Entity">compile</span>(prev_thing) }
  <span class="Keyword">end</span>
<span class="Keyword">end</span>
</pre>
<p>No magic at all (as long as you understand how inject works). I really like how Temple contains many small pieces which does one thing, and they all easily stacks up and produce some fairy good Ruby code.</p>
<h3>Another abstraction: Haml and HTML</h3>
<p>Okay, so I&#8217;ve shown you two examples: ERB which compiles directly to core, and Mustache which uses one abstraction. Now let&#8217;s have a look at Haml.</p>
<p>Because Haml is so complex, going directly to core can be difficult. Instead it might be smart to introduce an HTML-abstraction:</p>
<pre class="sunburst">
[<span class="Constant"><span class="Constant">:</span>multi</span>,
  [<span class="Constant"><span class="Constant">:</span>html</span>, <span class="Constant"><span class="Constant">:</span>tag</span>,
    <span class="Constant"><span class="Constant">:</span>a</span>,
    [<span class="Constant"><span class="Constant">:</span>basicattr</span>, <span class="Constant"><span class="Constant">:</span>href</span>, <span class="String"><span class="String">&quot;</span>http://judofyr.net/<span class="String">&quot;</span></span>],
    [<span class="Constant"><span class="Constant">:</span>static</span>, <span class="String"><span class="String">&quot;</span>Magnus Holm's blog<span class="String">&quot;</span></span>]]]  
</pre>
<p>There can be several HTML-compilers. One aims for speed (Haml&#8217;s ugly option), one that aims for pretty indentation (Haml&#8217;s pretty option). And I can use the same compiler in Parkaby. If one of these compilers improves and generates better code, it&#8217;s going to improve performance in both Haml and Parkaby. Performance wars are going to be so boring!</p>
<p>Nathan Weizenbaum (the maintainer of Haml) told me targeting Haml could be difficult, but I&#8217;m quite optimistic. It&#8217;s better to try, fail and learn, than not try at all. And to be honest, I&#8217;m willing to bend things around to make Haml a happy citizen in Temple.</p>
<p>I&#8217;d also love to see how Liquid can fit into this mix.</p>
<h3>The goal</h3>
<p>So what do I want with all this? I want to experiment! I want to see if it&#8217;s possible to improve on the lowest level of the abstractions, and I&#8217;d like to see how it affects the upper layers. I&#8217;d like to learn more about different template engines; how they work, how they are parsed, how they perform. I simply want to learn. And I want to share the knowledge.</p>
<p>I want people to experiment and create new concepts in templating. I want to see Domain-Specific Template-Languages. I want people to realize that creating a template engine is just like creating a programming language, just on a smaller scale. Testing frameworks are so boring, why don&#8217;t try to create The Perfect Template Language™?</p>
<p>And, of course, I want <strong>fast</strong> template engines that shares code which each other.</p>
<p>But in the end, it&#8217;s just an experiment. Maybe it&#8217;s successful, probably not, it doesn&#8217;t matter so much as long as I have fun.</p>
<h3>Join the fun!</h3>
<p>I&#8217;m the dumb one here, and if you know <em>anything</em> about template engines I&#8217;d love to hear your thoughts on Temple. In fact, I&#8217;d love to hear <em>everybody&#8217;s</em> thoughts on Temple!</p>
<p>If you&#8217;re interested, please join <a href="http://groups.google.com/group/guardians-of-the-temple">the mailing list</a>. The documentation is pretty non-existing at the moment, if there are any questions at all, please do not hesitate to ask.</p>
<p>The code is available at <a href="http://github.com/judofyr/temple">github.com/judofyr/temple</a>.</p>
<ul class="footnotes"><li id="fn1"><sup>1</sup> I wrote that code though.</li></ul>




]]></content>
  </entry>

  <entry>
    <title>Parkaby</title>
    <id>tag:judofyr.net,2009-07-04:1246711417</id>
    <link href="http://judofyr.net/posts/parkaby.html"/>
    <updated>2009-07-04T12:43:37Z</updated>
    <published>2009-07-04T12:43:37Z</published>
    <content type="html"><![CDATA[<pre class="sunburst">
<span class="Variable">Parkaby</span> {
  html {
    head {
      title <span class="String"><span class="String">&quot;</span>happy title<span class="String">&quot;</span></span>
    }
    body {
      h1 <span class="String"><span class="String">&quot;</span>happy heading<span class="String">&quot;</span></span>
      a <span class="String"><span class="String">&quot;</span>a link<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>href<span class="String">&quot;</span></span> =&gt; <span class="String"><span class="String">&quot;</span>url<span class="String">&quot;</span></span>
    }
  }
}
</pre>
<p><a href="http://github.com/judofyr/parkaby">http://github.com/judofyr/parkaby</a></p>
<pre>
~&gt; ruby bench/run.rb simple 10000

                           user     system      total        real
Erubis                 0.030000   0.000000   0.030000 (  0.022264)
Haml                   0.110000   0.000000   0.110000 (  0.117887)
Parkaby (def_method)   0.130000   0.000000   0.130000 (  0.135996)
Parkaby (render)       0.150000   0.010000   0.160000 (  0.150680)
Parkaby (inline)       0.970000   0.000000   0.970000 (  0.988010)
Tagz                   3.250000   0.040000   3.290000 (  3.400699)
Markaby               12.610000   0.140000  12.750000 ( 13.067794)


~&gt; ruby bench/run.rb nasty 500

                           user     system      total        real
Erubis                 0.190000   0.010000   0.200000 (  0.198487)
Parkaby (def_method)   0.350000   0.000000   0.350000 (  0.363106)
Parkaby (render)       0.360000   0.010000   0.370000 (  0.365007)
Parkaby (inline)       0.570000   0.000000   0.570000 (  0.614286)
Haml                   2.490000   0.030000   2.520000 (  2.620025)
Tagz                   5.100000   0.060000   5.160000 (  5.394778)
Markaby                7.220000   0.090000   7.310000 (  7.630588)
</pre>
<p>Feel free to ask questions in the comments.</p>]]></content>
  </entry>

  <entry>
    <title>Copy'n'Paste</title>
    <id>tag:judofyr.net,2009-06-05:1244205304</id>
    <link href="http://judofyr.net/posts/copy-paste-irb.html"/>
    <updated>2009-06-05T12:35:04Z</updated>
    <published>2009-06-05T12:35:04Z</published>
    <content type="html"><![CDATA[<p>Mac:</p>
<pre class="sunburst">
<span class="Comment"><span class="Comment">#</span> stick in .irbrc</span>
<span class="Keyword">def</span> <span class="Entity">copy</span>(<span class="Variable">str</span>)
  <span class="Variable">IO</span>.<span class="Entity">popen</span>(<span class="String"><span class="String">'</span>pbcopy<span class="String">'</span></span>, <span class="String"><span class="String">'</span>w<span class="String">'</span></span>) { |<span class="Variable">f</span>| f <span class="Keyword">&lt;&lt;</span> str.<span class="Entity">to_s</span> }
<span class="Keyword">end</span>

<span class="Keyword">def</span> <span class="Entity">paste</span>
  <span class="String"><span class="String">`</span>pbpaste<span class="String">`</span></span>
<span class="Keyword">end</span>

<span class="Keyword">def</span> <span class="Entity">ep</span>
  <span class="Entity">eval</span>(paste)
<span class="Keyword">end</span>
</pre>
<p>Linux with xclip (thanks <a href="http://gist.github.com/124272">Bjørn Arild Mæland</a>):</p>
<pre class="sunburst">
<span class="Keyword">def</span> <span class="Entity">copy</span>(<span class="Variable">str</span>)
  <span class="Variable">IO</span>.<span class="Entity">popen</span>(<span class="String"><span class="String">'</span>xclip -i<span class="String">'</span></span>, <span class="String"><span class="String">'</span>w<span class="String">'</span></span>) { |<span class="Variable">f</span>| f <span class="Keyword">&lt;&lt;</span> str.<span class="Entity">to_s</span> }
<span class="Keyword">end</span>
 
<span class="Keyword">def</span> <span class="Entity">paste</span>
  <span class="String"><span class="String">`</span>xclip -o<span class="String">`</span></span>
<span class="Keyword">end</span>
</pre>]]></content>
  </entry>

  <entry>
    <title>GitHub.js</title>
    <id>tag:judofyr.net,2009-05-28:1243528045</id>
    <link href="http://judofyr.net/posts/github-js.html"/>
    <updated>2009-05-28T16:27:25Z</updated>
    <published>2009-05-28T16:27:25Z</published>
    <content type="html"><![CDATA[<p><img src="http://www.quicksnapper.com/files/3880/6250845634A1EBC5B6F2A5_m.png" alt="" /></p>
<p>A few weeks ago, <a href="http://github.com/hakunin">hakunin</a> ruined my repo-followers-ratio, and in order to restore balance in my soul I now see no other options than to reveal one of the things I&#8217;ve been playing with lately: <a href="http://github.com/judofyr/github-js"><strong>GitHub.js</strong></a>.</p>
<p>It&#8217;s not very pretty, not very idiomatic and has a nasty dependency on JS.Class. Please remember that I&#8217;m still a novice when it comes to JavaScript, so feel free to <a href="http://github.com/judofyr/github-js">fork away</a> and turn this into something usable.</p>]]></content>
  </entry>

  <entry>
    <title>Nokogirl</title>
    <id>tag:judofyr.net,2009-03-04:1236186825</id>
    <link href="http://judofyr.net/posts/nokogirl.html"/>
    <updated>2009-03-04T17:13:45Z</updated>
    <published>2009-03-04T17:13:45Z</published>
    <content type="html"><![CDATA[<p>Every time I play with Nokogiri, I get this weird error:</p>
<pre>
$ irb -rnokogirl
no such file to load -- nokogirl (LoadError)
</pre>
<p>My head just can&#8217;t accept it&#8217;s called <em>nokogiri</em> instead of <em>nokogirl</em>. Hopefully, this should teach me:</p>
<pre>
$ sudo gem install nokogirl
Building native extensions.  This could take a while...

********************************************
It's actually spelled nokogiri, not nokogirl
********************************************

Successfully installed nokogiri-1.2.1
Successfully installed nokogirl-1.0
2 gems installed

$ irb -rnokogirl

********************************************
It's actually spelled nokogiri, not nokogirl
********************************************

&gt;&gt; Nokogirl
=&gt; Nokogiri
</pre>]]></content>
  </entry>

  <entry>
    <title>Change</title>
    <id>tag:judofyr.net,2009-02-08:1234119353</id>
    <link href="http://judofyr.net/posts/change.html"/>
    <updated>2009-02-08T18:55:53Z</updated>
    <published>2009-02-08T18:55:53Z</published>
    <content type="html"><![CDATA[<p>Every once in a while we need change. Not only when it comes to presidents and the placement of the telly, but also this specific blog. You see, I&#8217;m <em>proud</em> of this blog. I got subscribers and, once in a while, I get some decent traffic too.</p>
<p><img src="http://img.skitch.com/20090208-q6hmfc87smw59preu1r7i3x571.png" alt="" /></p>
<p>I&#8217;m also very satisfied with both the design and the concept. You start at the top and read until you reach the bottom. No sidebar. No distractions. The content <em>is</em> king. Black, white and red makes it clean; large fonts and line height makes it readable; narrow width makes the paragraphs look longer.</p>
<p>Still, it&#8217;s not perfect. The contrast in the header makes it stand out, almost drowning out the title of the post. The timestamps look like they were just throw on later (they were) and it&#8217;s pretty cramped in the &#8220;Wall of fame&#8221; at the bottom. And I haven&#8217;t styled &lt;h4&gt; at all.</p>
<hr>
<p>Welcome to the new version of <a href="http://judofyr.net">judofyr.net</a>! If you&#8217;re reading this from your feed reader, please come over and have a look. It&#8217;s a lot more cleaner: the title is definitly in focus, the timestamps blend nicely into the rest of the site and it&#8217;s a little more colorful with some blue and green.</p>
<p>It&#8217;s still not perfect. I&#8217;m not totally satisfied with the styling of the sub-headlines and the &#8220;Wall of fame&#8221; has maybe too low contrast. But hey, I&#8217;m a programmer &#8211; I&#8217;m not supposed to design sites.</p>
<p>So, readers, what do you think of this change? After all, this blog is worthless without you. Better? Worse?</p>]]></content>
  </entry>

  <entry>
    <title>Tailin' Ruby</title>
    <id>tag:judofyr.net,2009-01-25:1232892432</id>
    <link href="http://judofyr.net/posts/tailin-ruby.html"/>
    <updated>2009-01-25T14:07:12Z</updated>
    <published>2009-01-25T14:07:12Z</published>
    <content type="html"><![CDATA[<p>Ruby doesn&#8217;t do tail call optimization. That sucks. However, quite a few month ago I managed to fake it:</p>
<pre class="sunburst">
<span class="Variable">RunAgain</span> <span class="Keyword">=</span> <span class="Support">Class</span>.<span class="Entity">new</span>(<span class="Variable">Exception</span>)
<span class="Keyword">def</span> <span class="Entity">fib</span>(<span class="Variable">i<span class="Variable">,</span> n <span class="Keyword">=</span> <span class="Constant">1</span><span class="Variable">,</span> result <span class="Keyword">=</span> <span class="Constant">0</span></span>)
  <span class="Keyword">if</span> i <span class="Keyword">==</span> <span class="Keyword">-</span><span class="Constant">1</span>
    result
  <span class="Keyword">else</span>
    <span class="Keyword">raise</span> <span class="Variable">RunAgain</span>
  <span class="Keyword">end</span>
<span class="Keyword">rescue</span> <span class="Variable">RunAgain</span>
  i, n, result <span class="Keyword">=</span> i <span class="Keyword">-</span> <span class="Constant">1</span>, n <span class="Keyword">+</span> result, n
  <span class="Keyword">retry</span>
<span class="Keyword">end</span>
</pre>
<p>It was <strong>extremely</strong> slow (see benchmark at the end of this post) since it has to build backtrace for each raise, but at least it worked! I was proud; <em>very</em> proud. Until I discovered <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/145593">this snippet</a>:</p>
<pre class="sunburst">
<span class="Keyword">class</span> <span class="JEntityNameType">Class</span>
<span class="Comment">  <span class="Comment">#</span> Sweet stuff!</span>
  <span class="Keyword">def</span> <span class="Entity">tailcall_optimize</span>(<span class="Variable"> <span class="Keyword">*</span>methods </span>)
    methods.<span class="Entity">each</span> <span class="Keyword">do </span>|<span class="Variable">meth</span>|
      org <span class="Keyword">=</span> <span class="Entity">instance_method</span>( meth )
      <span class="Entity">define_method</span>( meth ) <span class="Keyword">do </span>|*<span class="Variable">args</span>|
        <span class="Keyword">if</span> <span class="Support">Thread</span>.<span class="Entity">current</span>[ meth ]
          <span class="Keyword">throw</span>( <span class="Constant"><span class="Constant">:</span>recurse</span>, args )
        <span class="Keyword">else</span>
          <span class="Support">Thread</span>.<span class="Entity">current</span>[ meth ] <span class="Keyword">=</span> org.<span class="Entity">bind</span>( <span class="Variable">self</span> )
          result <span class="Keyword">=</span> <span class="Keyword">catch</span>( <span class="Constant"><span class="Constant">:</span>done</span> ) <span class="Keyword">do</span>
            <span class="Keyword">loop</span> <span class="Keyword">do</span>
              args <span class="Keyword">=</span> <span class="Keyword">catch</span>( <span class="Constant"><span class="Constant">:</span>recurse</span> ) <span class="Keyword">do</span>
                <span class="Keyword">throw</span>( <span class="Constant"><span class="Constant">:</span>done</span>, <span class="Support">Thread</span>.<span class="Entity">current</span>[ meth ].<span class="Entity">call</span>( <span class="Keyword">*</span>args ) )
              <span class="Keyword">end</span>
            <span class="Keyword">end</span>
          <span class="Keyword">end</span>
          <span class="Support">Thread</span>.<span class="Entity">current</span>[ meth ] <span class="Keyword">=</span> <span class="Constant">nil</span>
          result
        <span class="Keyword">end</span>
      <span class="Keyword">end</span>
    <span class="Keyword">end</span>
  <span class="Keyword">end</span>
<span class="Keyword">end</span>

<span class="Keyword">class</span> <span class="JEntityNameType">TCOTest</span>
<span class="Comment">  <span class="Comment">#</span> tail-recursive factorial</span>
  <span class="Keyword">def</span> <span class="Entity">fact</span>(<span class="Variable"> n<span class="Variable">,</span> acc <span class="Keyword">=</span> <span class="Constant">1</span> </span>)
    <span class="Keyword">if</span> n <span class="Keyword">&lt;</span> <span class="Constant">2</span> <span class="Keyword">then</span> acc <span class="Keyword">else</span> <span class="Entity">fact</span>( n<span class="Keyword">-</span><span class="Constant">1</span>, n<span class="Keyword">*</span>acc ) <span class="Keyword">end</span>
  <span class="Keyword">end</span>

<span class="Comment">  <span class="Comment">#</span> length of factorial</span>
  <span class="Keyword">def</span> <span class="Entity">fact_size</span>(<span class="Variable"> n </span>)
    <span class="Entity">fact</span>( n ).<span class="Entity">size</span>
  <span class="Keyword">rescue</span>
    <span class="Variable"><span class="Variable">$</span>!</span>
  <span class="Keyword">end</span>   
<span class="Keyword">end</span>

t <span class="Keyword">=</span> <span class="Variable">TCOTest</span>.<span class="Entity">new</span>

<span class="Comment"><span class="Comment">#</span> normal method</span>
puts t.<span class="Entity">fact_size</span>( <span class="Constant">10000</span> )  <span class="Comment"><span class="Comment">#</span> =&gt; stack level too deep</span>

<span class="Comment"><span class="Comment">#</span> enable tail-call optimization</span>
<span class="Keyword">class</span> <span class="JEntityNameType">TCOTest</span>
  tailcall_optimize <span class="Constant"><span class="Constant">:</span>fact</span>
<span class="Keyword">end</span>

<span class="Comment"><span class="Comment">#</span> tail-call optimized method</span>
puts t.<span class="Entity">fact_size</span>( <span class="Constant">10000</span> )  <span class="Comment"><span class="Comment">#</span> =&gt; 14808                                  </span>
</pre>
<p>Not only was it faster, you could also drop it in wherever you want. Sweet stuff, indeed. My (failed) attempt was sent to /dev/null immediately&#8230;</p>
<h3>The Ruby Programming Language arrives</h3>
<p>A few months later, I received my copy of <a href="http://www.amazon.com/Ruby-Programming-Language-David-Flanagan/dp/0596516177">The Ruby Programming Language</a> and started reading it. Suddenly, on page 151:</p>
<blockquote>
<p><cite>The Ruby Programming Language, page 151, 5.5.4 redo</cite>: The <code>redo</code> statement restarts the current iteration of a loop or iterator. This is not the same thing as <code>next</code>. <code>next</code> transfers control to the end of a loop or block so that the next iteration can begin, whereas <code>redo</code> transfers control back to the top of the loop or block so that the iteration can start over. If you come to Ruby from a C-like language, then <code>redo</code> is probably a new control structure for you.</p>
</blockquote>
<p>I&#8217;ve used Ruby for a long time, still I&#8217;ve never heard of <code>redo</code>&#8230; Here&#8217;s an example from the book:</p>
<pre class="sunburst">
puts <span class="String"><span class="String">&quot;</span>Please enter the first word you think of<span class="String">&quot;</span></span>
words <span class="Keyword">=</span> <span class="String"><span class="String">%w(</span>apple banana cherry<span class="String">)</span></span>
response <span class="Keyword">=</span> words.<span class="Entity">collect</span> <span class="Keyword">do </span>|<span class="Variable">word</span>|
<span class="Comment">  <span class="Comment">#</span> Control returns here when redo is executed</span>
  print word <span class="Keyword">+</span> <span class="String"><span class="String">&quot;</span>&gt; <span class="String">&quot;</span></span>               <span class="Comment"><span class="Comment">#</span> Prompt the user</span>
  response <span class="Keyword">=</span> gets.<span class="Entity">chop</span>            <span class="Comment"><span class="Comment">#</span> Get a response</span>
  <span class="Keyword">if</span> response.<span class="Entity">size</span> <span class="Keyword">==</span> <span class="Constant">0</span>           <span class="Comment"><span class="Comment">#</span> If user entered nothing</span>
    word.<span class="Entity">upcase!</span>                  <span class="Comment"><span class="Comment">#</span> Emphasize the prompt with uppercase</span>
    <span class="Keyword">redo</span>                          <span class="Comment"><span class="Comment">#</span> And skip to the top of the block</span>
  <span class="Keyword">end</span>
  response                        <span class="Comment"><span class="Comment">#</span> Return the response</span>
<span class="Keyword">end</span>
</pre>
<p>So I started thinking: The reason I used raise/rescue in my implementation was because I needed the <code>retry</code>-keyword&#8230; Maybe&#8230; What if?</p>
<pre class="sunburst">
<span class="Keyword">def</span> <span class="Entity">fib</span>(<span class="Variable">i<span class="Variable">,</span> n <span class="Keyword">=</span> <span class="Constant">1</span><span class="Variable">,</span> result <span class="Keyword">=</span> <span class="Constant">0</span></span>)
  <span class="Keyword">if</span> i <span class="Keyword">==</span> <span class="Keyword">-</span><span class="Constant">1</span>
    result
  <span class="Keyword">else</span>
    i, n, result <span class="Keyword">=</span> i <span class="Keyword">-</span> <span class="Constant">1</span>, n <span class="Keyword">+</span> result, n
    <span class="Keyword">redo</span>
  <span class="Keyword">end</span>
<span class="Keyword">end</span>

<span class="Entity">fib</span>(<span class="Constant">10000</span>)
</pre>
<p>Unfortunately, &#8220;The <code>redo</code> statement restarts the currentiteration of a <strong>loop</strong> or <strong>iterator</strong>&#8221;, so it only throws a LocalJumpError. However, don&#8217;t forget that we&#8217;re dealing with Ruby:</p>
<pre class="sunburst">
<span class="Comment"><span class="Comment">#</span>## Everything is possible in Ruby!</span>

<span class="Entity">define_method</span>(<span class="Constant"><span class="Constant">:</span>acc</span>) <span class="Keyword">do </span>|<span class="Variable">i</span>, <span class="Variable">n</span>, <span class="Variable">result</span>|
  <span class="Keyword">if</span> i <span class="Keyword">==</span> <span class="Keyword">-</span><span class="Constant">1</span>
    result
  <span class="Keyword">else</span>
    i, n, result <span class="Keyword">=</span> i <span class="Keyword">-</span> <span class="Constant">1</span>, n <span class="Keyword">+</span> result, n
    <span class="Keyword">redo</span>
  <span class="Keyword">end</span>
<span class="Keyword">end</span>

<span class="Keyword">def</span> <span class="Entity">fib</span>(<span class="Variable">i</span>)
  <span class="Entity">acc</span>(i, <span class="Constant">1</span>, <span class="Constant">0</span>)
<span class="Keyword">end</span>

<span class="Entity">fib</span>(<span class="Constant">10000</span>)  <span class="Comment"><span class="Comment">#</span> Yeah!</span>
</pre>
<h3>The Benchmark</h3>
<p>So, how fast is it? Take a look below:</p>
<p><iframe src="http://judofyr.github.com/recursive/" style="width:660px;height:460px;border:0"></iframe></p>
<ul>
	<li>You can zoom by marking in any of the graphs.</li>
	<li>When you un-tick a graph, mark in the lower graph to zoom in.</li>
	<li>Un-tick everything else than &#8220;Redo&#8221; and &#8220;Iterative&#8221; and notice how close they are to each other.</li>
	<li>Look how bad &#8220;Rescue&#8221; performs,</li>
	<li>and how early &#8220;Regular&#8221; fails.</li>
	<li>All the code is available at <a href="http://github.com/judofyr/recursive">GitHub</a>.</li>
</ul>
<p>Nifty, eh?</p>

]]></content>
  </entry>

  <entry>
    <title>When in Doubt, Turn to _why</title>
    <id>tag:judofyr.net,2009-01-20:1232484292</id>
    <link href="http://judofyr.net/posts/when-in-doubt.html"/>
    <updated>2009-01-20T20:44:52Z</updated>
    <published>2009-01-20T20:44:52Z</published>
    <content type="html"><![CDATA[<p><a href="http://groups.google.com/group/rack-devel/browse_thread/thread/1a9b8dc431bff499">Rack can&#8217;t parse nested hash params.</a> That&#8217;s pretty annoying. So while we still can&#8217;t decide how to implement it in the best way, every other framework is cleaning up after Rack. My <a href="http://github.com/judofyr/camping/commit/95d2262c#L0R416">attempt</a> to do it in Camping is clearly a bad solution, so I decided to have a look at other implementations and steal some ideas.</p>
<h3>Let&#8217;s use Sinatra as an example</h3>
<p>Most of them are very similar to Sinatra&#8217;s (this is slightly modified for readability, but the idea still applies):</p>
<pre class="sunburst">
params.<span class="Entity">inject</span>({}) <span class="Keyword">do </span>|<span class="Variable">hash</span>, (<span class="Variable">key</span>, <span class="Variable">value</span>)|
  <span class="Keyword">if</span> key <span class="Keyword">=~</span> <span class="StringRegexp"><span class="StringRegexp">/</span></span><span class="StringRegexp"><span class="StringRegexpSpecial">\[</span>.*<span class="StringRegexpSpecial">\]</span></span><span class="StringRegexp"><span class="StringRegexp">/</span></span>
    parts <span class="Keyword">=</span> key.<span class="Entity">scan</span>(<span class="StringRegexp"><span class="StringRegexp">/</span></span><span class="StringRegexp"><span class="StringRegexp"><span class="StringRegexp">(</span>^<span class="StringRegexp"><span class="StringRegexp">[</span>^<span class="StringRegexpSpecial">\[</span><span class="StringRegexp">]</span></span>+<span class="StringRegexp">)</span></span>|<span class="StringRegexpSpecial">\[</span><span class="StringRegexp"><span class="StringRegexp">(</span><span class="StringRegexp"><span class="StringRegexp">[</span>^<span class="StringRegexpSpecial">\]</span><span class="StringRegexp">]</span></span>+<span class="StringRegexp">)</span></span><span class="StringRegexpSpecial">\]</span></span><span class="StringRegexp"><span class="StringRegexp">/</span></span>).<span class="Entity">flatten</span>.<span class="Entity">compact</span>
    head, last <span class="Keyword">=</span> parts[<span class="Constant">0</span>..<span class="Keyword">-</span><span class="Constant">2</span>], parts[<span class="Keyword">-</span><span class="Constant">1</span>]
    head.<span class="Entity">inject</span>(hash){ |<span class="Variable">s</span>,<span class="Variable">v</span>| s[v] <span class="Keyword">||=</span> {} }[last] <span class="Keyword">=</span> value
  <span class="Keyword">else</span>
    hash[key] <span class="Keyword">=</span> value
  <span class="Keyword">end</span>
  res
<span class="Keyword">end</span>   
</pre>
<p><strong>Update:</strong> As Ryan has written in the comments, I better give some credz to the real author of the snippet above: Sinatra&#8217;s nested params implementation was taken from an example posted to the Rack ML by <strong>Michael Fellinger</strong> (of Ramaze fame). For the background on the patch that went into Sinatra: take a look <a href="http://sinatra.lighthouseapp.com/projects/9779/tickets/70">the ticket</a> over at their bug tracker.</p>
<p>We&#8217;re looping through each of the params and doing some stuff if the key includes [something] and therefore is nested. If it&#8217;s nested, we need to find the parts:</p>
<pre class="sunburst">
parts <span class="Keyword">=</span> key.<span class="Entity">scan</span>(<span class="StringRegexp"><span class="StringRegexp">/</span></span><span class="StringRegexp"><span class="StringRegexp"><span class="StringRegexp">(</span>^<span class="StringRegexp"><span class="StringRegexp">[</span>^<span class="StringRegexpSpecial">\[</span><span class="StringRegexp">]</span></span>+<span class="StringRegexp">)</span></span>|<span class="StringRegexpSpecial">\[</span><span class="StringRegexp"><span class="StringRegexp">(</span><span class="StringRegexp"><span class="StringRegexp">[</span>^<span class="StringRegexpSpecial">\]</span><span class="StringRegexp">]</span></span>+<span class="StringRegexp">)</span></span><span class="StringRegexpSpecial">\]</span></span><span class="StringRegexp"><span class="StringRegexp">/</span></span>).<span class="Entity">flatten</span>.<span class="Entity">compact</span>
</pre>
<p>The first part of the regex matches from the beginning to the first [, and the second matches each of the [parts]. Some cleanup is needed to flatten and remove nils (just try the line in IRB and you&#8217;ll see it quickly).</p>
<p>Next up, we&#8217;re splitting out the last part, and then comes the inject:</p>
<pre class="sunburst">
head.<span class="Entity">inject</span>(hash){ |<span class="Variable">s</span>,<span class="Variable">v</span>| s[v] <span class="Keyword">||=</span> {} }[last] <span class="Keyword">=</span> value  
</pre>
<p>Here, we&#8217;re building a Hash based on the params we&#8217;ve already cleaned up and the current param we&#8217;re working on. Then, finally, we&#8217;re setting the value.</p>
<h3>The _why way</h3>
<p>All of this makes sense. I wrote approximately the same when trying to cleanup my broken version. However, while I was looking for the way Ramaze did it, I found an excellent link to RedHanded: <a href="http://redhanded.hobix.com/inspect/injectingAHashBackwardsAndTheMergeBlock.html">Injecting a Hash Backwards and the Merge Block</a>. That version is just <strong>awesome</strong> (this is also slightly modified):</p>
<pre class="sunburst">
m <span class="Keyword">=</span> proc {|<span class="Variable">_</span>,<span class="Variable">o</span>,<span class="Variable">n</span>|o.<span class="Entity">merge</span>(n,<span class="Keyword">&amp;</span>m)}
params.<span class="Entity">inject</span>({}) <span class="Keyword">do </span>|<span class="Variable">hash</span>, (<span class="Variable">key</span>, <span class="Variable">value</span>)|
  parts <span class="Keyword">=</span> key.<span class="Entity">split</span>(<span class="StringRegexp"><span class="StringRegexp">/</span></span><span class="StringRegexp"><span class="StringRegexp"><span class="StringRegexp">[</span><span class="StringRegexpSpecial">\]</span><span class="StringRegexpSpecial">\[</span><span class="StringRegexp">]</span></span>+</span><span class="StringRegexp"><span class="StringRegexp">/</span></span>)
  hash.<span class="Entity">merge</span>(parts.<span class="Entity">reverse</span>.<span class="Entity">inject</span>(value) { |<span class="Variable">x</span>, <span class="Variable">i</span>| {i =&gt; x} }, <span class="Keyword">&amp;</span>m) 
<span class="Keyword">end</span>    
</pre>
<p>Notice the sweet, micro way to split out the parts: If we got a key like: &#8220;first[second][third]&#8221; we can simply split by any numbers of [ and ]. Of course, we&#8217;re going to fail on stuff like &#8220;this]is]really]one]key&#8221;, but if you&#8217;re writing like that, you deserve it!</p>
<p>Now, the next is what makes this so different and awesome. Let&#8217;s look at first part of it:</p>
<pre class="sunburst">
parts.<span class="Entity">reverse</span>.<span class="Entity">inject</span>(value) { |<span class="Variable">x</span>, <span class="Variable">i</span>| {i =&gt; x} }
</pre>
<p>We&#8217;re <strong>reversing</strong> it and building it backwards. The inject starts with our original value and then we build our way out by creating Hashes:</p>
<pre class="sunburst">
parts <span class="Keyword">=</span> [<span class="String"><span class="String">&quot;</span>first<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>second<span class="String">&quot;</span></span>, <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span>]
value <span class="Keyword">=</span> <span class="Constant">123</span>

<span class="Comment"><span class="Comment">#</span> first run of inject:</span>
x <span class="Keyword">=</span> <span class="Constant">123</span>
i <span class="Keyword">=</span> <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span>
<span class="Keyword">return</span> { <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span> =&gt; <span class="Constant">123</span> }

<span class="Comment"><span class="Comment">#</span> second run of inject:</span>
x <span class="Keyword">=</span> { <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span> =&gt; <span class="Constant">123</span> }
i <span class="Keyword">=</span> <span class="String"><span class="String">&quot;</span>second<span class="String">&quot;</span></span>
<span class="Keyword">return</span> { <span class="String"><span class="String">&quot;</span>second<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span> =&gt; <span class="Constant">123</span> } }

<span class="Comment"><span class="Comment">#</span> second run of inject:</span>
x <span class="Keyword">=</span> { <span class="String"><span class="String">&quot;</span>second<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span> =&gt; <span class="Constant">123</span> } }
i <span class="Keyword">=</span> <span class="String"><span class="String">&quot;</span>first<span class="String">&quot;</span></span>
<span class="Keyword">return</span> { <span class="String"><span class="String">&quot;</span>first<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>second<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span> =&gt; <span class="Constant">123</span> } } }
</pre>
<p>So when we got this little recursive Hash, we need to merge it with the rest. Most of you, including me, would say that it would be a hard task, since we also need to merge it recursively:</p>
<pre class="sunburst">
params <span class="Keyword">=</span> { <span class="String"><span class="String">&quot;</span>first<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>second<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span> =&gt; <span class="Constant">123</span> } } }
current_param <span class="Keyword">=</span> { <span class="String"><span class="String">&quot;</span>first<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>another<span class="String">&quot;</span></span> =&gt; <span class="Constant">456</span> } }

params.<span class="Entity">merge</span>(current_param) <span class="Comment"><span class="Comment">#</span> fail!</span>
current_param.<span class="Entity">merge</span>(params) <span class="Comment"><span class="Comment">#</span> fail!</span>
</pre>
<h3>Meet the Merge Block</h3>
<p>This was the first time I&#8217;ve ever <em>heard</em> of the merge block. It&#8217;s not even properly documented! But it&#8217;s a very simple and very powerful feature: If we get a merge conflict (same keys in both Hashes), it&#8217;s calling that block. Easy peasy!</p>
<pre class="sunburst">
params <span class="Keyword">=</span> { <span class="String"><span class="String">&quot;</span>first<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>second<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span> =&gt; <span class="Constant">123</span> } } }
current_param <span class="Keyword">=</span> { <span class="String"><span class="String">&quot;</span>first<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>another<span class="String">&quot;</span></span> =&gt; <span class="Constant">456</span> } }

params.<span class="Entity">merge</span>(current_param) <span class="Keyword">do </span>|<span class="Variable">key</span>, <span class="Variable">value_from_params</span>, <span class="Variable">value_from_current_param</span>|
<span class="Comment">  <span class="Comment">#</span> key is defined in both params and current_param</span>
  value_from_params.<span class="Entity">merge</span>(value_from_current_param)
<span class="Keyword">end</span>

<span class="Comment"><span class="Comment">#</span> =&gt;</span>
{ <span class="String"><span class="String">&quot;</span>first<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>second<span class="String">&quot;</span></span> =&gt; { <span class="String"><span class="String">&quot;</span>third<span class="String">&quot;</span></span> =&gt; <span class="Constant">123</span> }, <span class="String"><span class="String">&quot;</span>another<span class="String">&quot;</span></span> =&gt; <span class="Constant">456</span> } }
</pre>
<p>And in order to merge it recursively, we build the block in advance and pass the block into the inner merge too:</p>
<pre class="sunburst">
m <span class="Keyword">=</span> proc {|<span class="Variable">_</span>,<span class="Variable">o</span>,<span class="Variable">n</span>|o.<span class="Entity">merge</span>(n,<span class="Keyword">&amp;</span>m)}
<span class="Comment"><span class="Comment">#</span> ...</span>
hash.<span class="Entity">merge</span>(recursive_hash, <span class="Keyword">&amp;</span>m) 
</pre>
<h3>Shorter? Faster?</h3>
<p>I believe the first, natural way is both faster and shorter (if we use the micro splitter), but I still think I&#8217;m going to put _why&#8217;s version in Camping. Not (only) because it&#8217;s _why&#8217;s, but also because it&#8217;s so wicked awesome and fits perfectly together with the other weird stuff in Camping. Some bytes have to be sacrificed for style; Camping is still far away from exceeding the limit.</p>


]]></content>
  </entry>

  <entry>
    <title>Copy Folders to a Branch</title>
    <id>tag:judofyr.net,2008-12-20:1229795434</id>
    <link href="http://judofyr.net/posts/copy-folders-to-a-branch.html"/>
    <updated>2008-12-20T17:50:34Z</updated>
    <published>2008-12-20T17:50:34Z</published>
    <content type="html"><![CDATA[<p><a href="http://github.com/blog/272">GitHub Pages</a> is pretty cool. However, what do you do when you already got your website/docs/whatever in your master-branch? If you checkout the gh-pages branch, the folder disappears and you actually have to copy it to another place before you copy it to your new branch.</p>
<p>If it&#8217;s an automatically generated folder, it&#8217;s a bit easier, but still quite a few commands:</p>
<pre>
rake generate_some_docs
git checkout gh-pages
git add .
git commit
git push
</pre>
<p>It&#8217;s <a href="http://pages.github.com/">even harder</a> if you haven&#8217;t created the gh-pages branch yet.</p>
<h3>Grancher!</h3>
<p>Okey, enter Grancher (yeah, I know, lame name):</p>
<pre class="sunburst">
<span class="Comment"><span class="Comment">#</span> Install:</span>
<span class="Entity">system</span>(<span class="String"><span class="String">'</span>sudo gem install grancher<span class="String">'</span></span>)

<span class="Comment"><span class="Comment">#</span> Create a Rakefile:</span>
<span class="Keyword">require</span> <span class="String"><span class="String">'</span>grancher/task<span class="String">'</span></span>
<span class="Support">Grancher</span>::<span class="Entity">Task</span>.<span class="Entity">new</span> <span class="Keyword">do </span>|<span class="Variable">g</span>|
  g.<span class="Entity">branch</span> <span class="Keyword">=</span> <span class="String"><span class="String">'</span>gh-pages<span class="String">'</span></span>
  g.<span class="Entity">push_to</span> <span class="Keyword">=</span> <span class="String"><span class="String">'</span>origin<span class="String">'</span></span> <span class="Comment"><span class="Comment">#</span> automatically push too</span>
  
  g.<span class="Entity">directory</span> <span class="String"><span class="String">'</span>website<span class="String">'</span></span>
<span class="Keyword">end</span>
</pre>
<p>Do a <code>rake publish</code> and the folder will be copied to the gh-pages branch, committed and pushed. Easy peasy!</p>
<p>Have a look at <a href="http://judofyr.github.com/grancher">the documentation</a> which isn&#8217;t actually perfect, but should be enough to get started.</p>
<p>Oh, and it&#8217;s just a few hours old, so bugs will probably bite you! <em>When</em> they do, please report it at <a href="http://dojo.lighthouseapp.com/projects/22240-grancher/overview">lighthouse</a>.</p>


]]></content>
  </entry>
</feed>
