<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>The timeless repository</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://timeless.judofyr.net/changelog.xml"/>
  <link rel="alternate" type="text/html" href="http://timeless.judofyr.net/"/>
  <updated>2020-12-09T11:03:57-05:00</updated>
  <author>
    <name>Magnus Holm</name>
    <email>judofyr@gmail.com</email>
    <uri>http://judofyr.net/</uri>
  </author>
  <author>
    <name>Steve Klabnik</name>
    <email>steve@steveklabnik.com</email>
    <uri>http://www.steveklabnik.com/</uri>
  </author>
  
  <entry>
    <title>Abstraction Creep</title>
    <id>tag:timeless.judofyr.net,2012-05-25:1337969960</id>
    <link href="http://timelessrepo.com/abstraction-creep"/>
    <updated>2012-05-25T18:19:20Z</updated>
    <published>2012-05-25T18:19:20Z</published>
    <content type="html"><![CDATA[
<p>Abstractions are very powerful when you’re writing software. You don’t want to write your web applications in assembly, or an operating system in JavaScript for that matter. Choosing the right abstraction makes you focus on the real challenges and lets you forget the insignificant details.</p>

<p>Not all abstractions are perfect. Some are leaky; some are just plain stupid. But wait! We can just introduce an abstraction on top of that, right? Let’s hide the stupid details and provide a new, better abstraction!</p>

<p><em>Abstraction creep</em> is what happens when you try to save yourself from a shaky abstraction by introducing <em>another</em> abstraction, instead of just fixing that goddamn abstraction.</p>

<p>I’m going to use examples from Ruby and Ruby on Rails, but I think the examples are clear enough for any programmer to understand. Please <a href="/comments">contact</a> me if something is unclear.</p>

<h2 id="controller_tests">Controller tests</h2>

<p>Controller tests in Rails are slow. Or, more correctly: Starting the environment where you can run controller tests in Rails takes time. We’re talking 5-10 seconds here. 10 seconds doesn’t sound so bad, but when you’re trying to quickly iterate over test/code, waiting 10 seconds before getting any feedback kills your flow.</p>

<p>Well, luckily we can introduce another abstraction!</p>

<h3 id="the_old_code">The old code</h3>

<div><span class="k">class</span> <span class="nc">PicturesController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
  <span class="n">before_filter</span> <span class="ss">:required_login</span>
  
  <span class="k">def</span> <span class="nf">create</span>
    <span class="vi">@picture</span> <span class="o">=</span> <span class="n">current_user</span><span class="p">.</span><span class="nf">pictures</span><span class="p">.</span><span class="nf">create</span><span class="p">(</span><span class="n">params</span><span class="p">[</span><span class="ss">:picture</span><span class="p">])</span>
    <span class="k">if</span> <span class="vi">@picture</span><span class="p">.</span><span class="nf">persisted?</span>
      <span class="n">redirect_to</span> <span class="vi">@picture</span>
    <span class="k">else</span>
      <span class="n">render</span> <span class="ss">:action</span> <span class="o">=&gt;</span> <span class="ss">:edit</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<h3 id="lets_objectify">Let’s Objectify!</h3>

<p>Let’s use <a href="https://github.com/bitlove/objectify">Objectify</a> to make this controller more testable.</p>

<p>First, we separate out the filter:</p>

<div><span class="k">class</span> <span class="nc">RequiresLoginPolicy</span>
  <span class="k">def</span> <span class="nf">allowed?</span><span class="p">(</span><span class="n">current_user</span><span class="p">)</span> 
    <span class="o">!</span><span class="n">current_user</span><span class="p">.</span><span class="nf">nil?</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>Then, the actual action:</p>

<div><span class="k">class</span> <span class="nc">PicturesCreateService</span>
  <span class="k">def</span> <span class="nf">call</span><span class="p">(</span><span class="n">current_user</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
    <span class="n">current_user</span><span class="p">.</span><span class="nf">pictures</span><span class="p">.</span><span class="nf">create</span> <span class="n">params</span><span class="p">[</span><span class="ss">:picture</span><span class="p">]</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>And finally, the rendering:</p>

<div><span class="k">class</span> <span class="nc">PicturesCreateResponder</span>
  <span class="k">def</span> <span class="nf">call</span><span class="p">(</span><span class="n">service_result</span><span class="p">,</span> <span class="n">controller</span><span class="p">,</span> <span class="n">renderer</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">service_result</span><span class="p">.</span><span class="nf">persisted?</span>
      <span class="n">renderer</span><span class="p">.</span><span class="nf">redirect_to</span> <span class="n">service_result</span>
    <span class="k">else</span>
      <span class="n">renderer</span><span class="p">.</span><span class="nf">data</span><span class="p">(</span><span class="n">service_result</span><span class="p">)</span>
      <span class="n">renderer</span><span class="p">.</span><span class="nf">render</span> <span class="ss">:action</span> <span class="o">=&gt;</span> <span class="ss">:edit</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>Now we can test each of these three classes without loading Rails. Woah, super fast tests! They’re even <em>unit</em> tests; only testing a single step of the flow, so your tests become simpler too.</p>

<p>Win/win, right?</p>

<h3 id="lets_take_a_step_back">Let’s take a step back</h3>

<p>What have we actually done here?</p>

<p>We’ve created our own abstraction on top of Rails’ request/response-loop. Is it useful? Yes, we can now focus on smaller units. Does it have some problems? Yes, you can’t easily test the way the units actually work in production. Is it more complex? Hell, yes. We have more classes, more methods, more <code>end</code>s, more lines of code.</p>

<p>Is it worth it? I doubt it.</p>

<h3 id="fixing_the_abstraction">Fixing the abstraction</h3>

<p>Okay, so what’s the real problem here?</p>

<p>Controller tests are slow. Why?</p>

<p>Because loading controllers are slow. Why?</p>

<p>Because controllers are too complex; they depend on too many different parts of Rails which needs to be loaded at startup.</p>

<p>Why are controllers complex? A controller is rather simple:</p>

<ul>
<li>It takes a request and an empty response</li>

<li>It lets you define actions which change the response</li>

<li>It defines some convenience methods</li>
</ul>

<p>These are the most important features. Let’s write a very simple controller:</p>

<div><span class="k">class</span> <span class="nc">BasicController</span>
  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">responder</span><span class="p">)</span>
    <span class="vi">@request</span> <span class="o">=</span> <span class="n">request</span>
    <span class="vi">@responder</span> <span class="o">=</span> <span class="n">response</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">params</span>
    <span class="vi">@request</span><span class="p">.</span><span class="nf">params</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">render</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
    <span class="vi">@responder</span><span class="p">.</span><span class="nf">render</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">redirect_to</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
    <span class="vi">@responder</span><span class="p">.</span><span class="nf">redirect_to</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span>
  <span class="k">end</span>
  
  <span class="c1"># and so on...</span>
<span class="k">end</span>

<span class="c1"># Our previous code:</span>
<span class="k">class</span> <span class="nc">PicturesController</span> <span class="o">&lt;</span> <span class="no">BasicController</span>
  <span class="k">def</span> <span class="nf">current_user</span><span class="p">;</span> <span class="o">...</span> <span class="k">end</span>

  <span class="k">def</span> <span class="nf">create</span>
    <span class="vi">@picture</span> <span class="o">=</span> <span class="n">current_user</span><span class="p">.</span><span class="nf">pictures</span><span class="p">.</span><span class="nf">create</span><span class="p">(</span><span class="n">params</span><span class="p">[</span><span class="ss">:picture</span><span class="p">])</span>
    <span class="k">if</span> <span class="vi">@picture</span><span class="p">.</span><span class="nf">persisted?</span>
      <span class="n">redirect_to</span> <span class="vi">@picture</span>
    <span class="k">else</span>
      <span class="n">render</span> <span class="ss">:action</span> <span class="o">=&gt;</span> <span class="ss">:edit</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>No dependencies (i.e. no other files/classes to load), no magic. Super fast tests again:</p>

<div><span class="nb">test</span> <span class="s2">"uploading a new picture"</span> <span class="k">do</span>
  <span class="n">request</span> <span class="o">=</span> <span class="no">Request</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">:params</span> <span class="o">=&gt;</span> <span class="p">{</span> <span class="ss">:picture</span> <span class="o">=&gt;</span> <span class="p">{</span> <span class="o">...</span> <span class="p">}</span> <span class="p">})</span>
  <span class="n">responder</span> <span class="o">=</span> <span class="no">MockResponder</span><span class="p">.</span><span class="nf">new</span>
  
  <span class="n">picture</span> <span class="o">=</span> <span class="no">PicturesController</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="n">responder</span><span class="p">)</span>
  <span class="n">picture</span><span class="p">.</span><span class="nf">create</span>

  <span class="n">assert</span> <span class="n">response</span><span class="p">.</span><span class="nf">redirected_to</span> <span class="sr">/^/</span><span class="n">pictures</span><span class="o">/</span><span class="p">(\</span><span class="n">d</span><span class="o">+</span><span class="p">)</span><span class="o">/</span>
  <span class="c1"># or whatever we want to assert for</span>
<span class="k">end</span></div>

<p>Notice that I’m using the regular Request class. Creating Request objects should be just as easy as creating a new controller object. They are both very simple classes that have few dependencies.</p>

<h3 id="what_about_filters">What about filters?</h3>

<p>You might have noticed that I silently dropped the before_filter in the last example. This was intentional because <em>filters are not crucial to what a controller is doing</em>. That doesn’t mean they don’t belong there, it just means you need to figure out if it’s worth the added complexity.</p>

<p>Does it belong another place? Maybe in the router (yes, there are frameworks that do the filtering in the router)? Somewhere else? I don’t know. Maybe filters are lightweight and useful enough to take up some controller complexity.</p>

<p>And <em>this</em> is what we should focus on: Figuring out what belongs in the most basic classes in our framework. Not creating a horrible patchwork by adding more abstractions.</p>

<h3 id="disclaimer">Disclaimer</h3>

<p>Just to make it clear: This is by no means an argument against Objectify itself, just the part where they introduce another layer on top of ActionController. The concept of service objects <em>does</em> make sense in more complex applications, but I believe it’s possible to achieve it within the current abstractions exposed by Rails.</p>

<h2 id="models">Models</h2>

<p>Models in Rails have the same problem as controllers: They are too complex. Too many dependencies.</p>

<div><span class="k">class</span> <span class="nc">Post</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
<span class="k">end</span></div>

<p>It looks so simple, but in reality it’s a complicated beast:</p>

<ul>
<li>You need a database connection to just instantiate it (!)</li>

<li>The table “posts” needs to exists in the database</li>

<li>It adds a bunch of methods</li>
</ul>

<p>Models in Rails works great as long as you’re using them inside Rails’ request/response-loop where everything is set up for you. The moment you step out from that loop (tests, other libraries) it’s a big hassle.</p>

<p>It’s very tempting to add abstractions on top of ActiveRecord to make it easier to work with. Or you could fix the abstraction by using the <a href="http://martinfowler.com/eaaCatalog/dataMapper.html">Data Mapper</a> pattern instead.</p>

<h2 id="strive_for_simple_data_objects">Strive for simple data objects</h2>

<p>Abstraction creep often occurs when the data objects are too complex. And there’s <em>nothing</em> about data that makes it complex. Data is really, really simple. There’s only two things you need to do with data:</p>

<ol>
<li>You need to be able to combine data into one structure</li>

<li>You need to be able to take the data apart again</li>
</ol>

<p>If you’re not able to do those things easily, you’ve already lost the war. When the data is difficult to work with, you’re forced to add abstractions, dependency-injection, mocks, stubs, and whatnot. Instead of just working with the actual data, you’ll have to abstract away the data.</p>

<p>Please be aware that this is by no means “incompatible” with object-oriented programming. You can still get your precious information hiding. You can still override accessors so the interface you expose isn’t the same as the internal interface. You can still subclass your data objects. You can still add convenience methods for working with the data.</p>

<p>You just have to make it effortless to do the two most important things:</p>

<ol>
<li>Construct the data object</li>

<li>Access the data it’s composed of</li>
</ol>

<p>As long as you can do this (hopefully with no dependencies at all), you can actually use your data objects in other classes. In fact, it <em>encourages you</em> to create separate classes for separate problems. When you can easily use your data objects it’s even easier to separate functionality to other classes (as opposed to adding more functionality to the same class).</p>

<h2 id="sometimes_change_is_imminent">Sometimes change is imminent</h2>

<p>Of course, using complex data objects is far from the only way to create shaky abstractions. Sometimes we don’t even realize it’s flawed until it’s too late. Or maybe the world changes in such way that it invalidates the abstraction.</p>

<p>Rack is a typical example of an abstraction that hasn’t handled change well. Rack tries to abstract away HTTP in a simple and clear way. When Rack was introduced in 2007, the web was pretty much stateless and every request expected an response as soon as possible. A synchronous interface makes sense:</p>

<div><span class="c1"># When the web server sees a request it runs this:</span>
<span class="n">env</span> <span class="o">=</span> <span class="n">build_env_from_request</span>
<span class="n">status</span><span class="p">,</span> <span class="n">headers</span><span class="p">,</span> <span class="n">body</span> <span class="o">=</span> <span class="no">App</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
<span class="n">server_response</span><span class="p">(</span><span class="n">status</span><span class="p">,</span> <span class="n">headers</span><span class="p">,</span> <span class="n">body</span><span class="p">)</span></div>

<p>The web has <em>changed</em>. Suddenly you have long-polling, where the request can wait for up to 30 seconds before the server returns a response. We have WebSocket which completely breaks the stateless request/response-cycle. The requirements have changed.</p>

<p>Rack wasn’t designed to handle these issues, so another abstraction was built on top:</p>

<div><span class="k">def</span> <span class="nc">App</span><span class="o">.</span><span class="nf">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
  <span class="n">cb</span> <span class="o">=</span> <span class="n">env</span><span class="p">[</span><span class="s1">&#39;async.callback&#39;</span><span class="p">]</span>

  <span class="c1"># Async call</span>
  <span class="n">wait_for_long_polling</span> <span class="k">do</span>
    <span class="c1"># Now we can return the request!</span>
    <span class="n">cb</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="mi">200</span><span class="p">,</span> <span class="p">{},</span> <span class="p">[])</span>
  <span class="k">end</span>

  <span class="c1"># Send a dummy "response" in order to be "complient" with the Rack</span>
  <span class="c1"># abstraction.</span>
  <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="p">{},</span> <span class="p">[]]</span>
<span class="k">end</span></div>

<p>This is a horrible, hacky abstraction, implemented by some Ruby web servers (outside of the Rack specification). Not only does it <em>look</em> weird, it also breaks the whole Rack abstraction. Any layer between the web server and the application (that follows the Rack specification) will behave incorrectly when it sees the dummy response.</p>

<p>You can’t predict the future, and some abstractions are perfect until the future comes and ruins everything.</p>

<h2 id="sustain_the_pain_go_with_the_flow_or_deal_with_the_real_problem">Sustain the pain; go with the flow. Or: deal with the real problem.</h2>

<p>Sometimes you’re stuck with crappy abstractions. ActiveRecord isn’t perfect. ActionController has its problems. You <em>will</em> feel a slight pain when you’re forced to use them in a way you don’t like. It’s very tempting to just add another abstraction. It can’t harm, right?</p>

<p>Personally, I try to <em>avoid</em> these abstractions-of-abstractions for as long as possible. I find that the complexity introduced is hardly worth the advantages. I can accept some slow tests. I can work around annoyances. All in the name of simplicity, consistency and familiarity.</p>

<p>Maybe you’ll reach a point of anger. You can no longer handle the pain. Or maybe the abstraction is just impossible to work around (you can’t do long-polling efficiently in Rack without changing anything). If that happens, you might be better served to <strong>rewrite the current (shaky) abstraction, instead of building another layer in an already-unstable foundation</strong>.</p>

<p>When you decide to abstract away the abstraction, it means you have the <strong>competence to realize that the abstraction is wrong, but not the courage to fix the actual problem</strong>. Your choice might seem sensible now (easier to integrate with the old abstraction; less changes overall), but 5 months, 5 years or even 50 years from now, people are going to say: “Why do I have to care about this stuff? Why couldn’t you just have fixed the goddamn problem?”</p>
]]></content>
  </entry>
  <entry>
    <title>JSON: The JavaScript subset that isn't</title>
    <id>tag:timeless.judofyr.net,2011-05-15:1305474965</id>
    <link href="http://timelessrepo.com/json-isnt-a-javascript-subset"/>
    <updated>2011-05-15T15:56:05Z</updated>
    <published>2011-05-15T15:56:05Z</published>
    <content type="html"><![CDATA[
<p>From Wikipedia’s article on <a href="http://en.wikipedia.org/wiki/JSON">JSON</a></p>

<blockquote>
<p>JSON was based on a subset of the JavaScript scripting language.</p>

<p>All JSON-formatted text is also syntactically legal JavaScript code.</p>

<p>JSON is a subset of JavaScript.</p>
</blockquote>

<p>All these years we’ve heard it over and over again: “JSON is a JavaScript subset”. Guess what? They’re wrong. Wrong, wrong, wrong. You see, the devil’s in the details, and there’s no way to avoid it: Not <em>all</em> JSON-formatted text is legal JavaScript code:</p>

<pre><code>{&quot;JSON&quot;:&quot;ro cks!&quot;}</code></pre>

<p>Copy the <em>exact</em> code above and paste it into Firebug or the Web Inspector within a pair of parentheses (required to avoid an ambiguity in JavaScript’s syntax):</p>

<p><img src="http://stuff.judofyr.net/json/json-sucks.png" alt="JSON sucks" /></p>

<p>Wait, what? <code>SyntaxError: Unexpected token ILLEGAL</code>? That doesn’t make any sense! It’s just a regular object literal, how can <em>that</em> be a SyntaxError?</p>

<p>Try the same code in a proper JSON parser. No problems at all.</p>

<p>Of course it’s not <em>just</em> a regular object literal. There’s a sneaky little Unicode character in there too: Right between “ro” and “cks!” there’s a tiny <strong>U+2028</strong>. Your browser probably doesn’t display it because it’s whitespace: <a href="http://www.fileformat.info/info/unicode/char/2028/index.htm">LINE SEPARATOR</a>, but it’s still there. If you replace the character with a U+2029 (<a href="http://www.fileformat.info/info/unicode/char/2029/index.htm">PARAGRAPH SEPARATOR</a>) you would have the exact same issue.</p>

<h2 id="json__u2028__">JSON + U+2028 = ☺</h2>

<p>According to the JSON specification, you can safely use this character in any string. It’s not a quote, not a backslash, and not a <em>control character</em>. It’s just a weird Unicode whitespace character:</p>

<p><img src="http://stuff.judofyr.net/json/json-string.gif" alt="JSON string specification" /></p>

<h2 id="javascript__u2028__">JavaScript + U+2028 = ☹</h2>

<p>ECMA-262 (the standard that JavaScript is based on) on the other hand defines strings a little differently: According to <a href="http://bclary.com/2004/11/07/#a-7.8.4">7.8.4 String Literals</a>, a string can contain anything as long as it’s not a quote, a backslash or a <em>line terminator</em>:</p>

<pre><code>DoubleStringCharacter ::
    SourceCharacter but not double-quote &quot; or backslash \ or LineTerminator 
    \ EscapeSequence

SingleStringCharacter ::
    SourceCharacter but not single-quote &#39; or backslash \ or LineTerminator 
    \ EscapeSequence</code></pre>

<p>And what is a line terminator? Let’s have a look at <a href="http://bclary.com/2004/11/07/#a-7.3">7.3 Line Terminators</a>:</p>

<blockquote>
<p>The following characters are consider to be line terminators:</p>

<ul>
<li><code>\u000A</code> - Line Feed</li>

<li><code>\u000D</code> - Carriage Return</li>

<li><code>\u2028</code> - Line separator</li>

<li><code>\u2029</code> - Paragraph separator</li>
</ul>
</blockquote>

<p>Ouch.</p>

<p>No string in JavaScript can contain a literal U+2028 or a U+2029.</p>

<h2 id="so_what">So what?</h2>

<p>Because of these two invisible Unicode characters, JSON is <strong>not</strong> a subset of JavaScript. Close, but no cigar.</p>

<p>In most applications, you won’t notice this issue. First of all, the line separator and the paragraph separator aren’t exactly widely used. Secondly, any <em>proper</em> JSON parser will have no problems with parsing it.</p>

<p>However, when you’re dealing with <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> there’s no way around: You’re forced to use the JavaScript parser in the browser. And if you’re sending data that other have entered, a tiny U+2028 or U+2029 might sneak in and break your pretty cross-domain API.</p>

<h2 id="the_solution">The solution</h2>

<p>Luckily, the solution is simple: If we look at the JSON specification we see that the <em>only</em> place where a U+2028 or U+2029 can occur is in a string. Therefore we can simply replace every U+2028 with <code>\u2028</code> (the escape sequence) and U+2029 with <code>\u2029</code> whenever we need to send out some JSONP.</p>

<p>It’s already been <a href="https://github.com/rack/rack-contrib/pull/37">fixed in Rack::JSONP</a> and I encourage all frameworks or libraries that send out JSONP to do the same. It’s a one-line patch in most languages and the result is still 100% valid JSON.</p>
]]></content>
  </entry>
  <entry>
    <title>Making Ruby Gems</title>
    <id>tag:timeless.judofyr.net,2011-02-06:1297028181</id>
    <link href="http://timelessrepo.com/making-ruby-gems"/>
    <updated>2011-02-06T21:36:21Z</updated>
    <published>2011-02-06T21:36:21Z</published>
    <content type="html"><![CDATA[
<p>One of my favorite aspects of the Ruby community is its strong commitment to Open Source. Thousands of Rubyists have written great libraries and placed the code up on GitHub (or elsewhere on the Internet) for everyone to use. Ruby has a fantastic tool to distribute these libraries: RubyGems. One of the reasons that Open Source runs so strong through Ruby’s veins is how easy it is to share your code.</p>

<p>While making a gem is really easy, there are a few additional concerns that you should be aware of when you distribute your code as a gem. Here’s an example of building a simple Gem, with some notes on best practices along the way.</p>

<h2 id="the_structure_of_a_gem">The structure of a gem</h2>

<p>At its core, a gem consists of two things: some <em>code</em>, and a <em>gemspec</em>. The gemspec file defines a <code>Gem::Specification</code> object, which is used by <code>rubygems</code> to handle the management of your code. That’s it! Nothing complicated. However, there are some conventions that virtually all gems follow that will help you out.</p>

<p>Most gems have a directory structure that looks like this:</p>

<pre><code>$ tree -d mygem
|-- lib/
|   |-- mygem.rb
|   `-- mygem/
|       |-- base.rb
|       `-- version.rb
|-- test/
|-- bin/
|-- Rakefile
`-- mygem.gemspec</code></pre>

<p>All of the code for the gem goes under the <code>lib</code> directory. This directory is what gets added to your $LOAD_PATH, and so <code>lib</code> usually contains two things: mygem.rb and a directory named mygem. mygem.rb will look like this:</p>

<div><span class="nb">require</span> <span class="s1">&#39;mygem/base&#39;</span></div>

<p>If there were more files in <code>lib/mygem</code>, they’d be <code>require</code>d here, too. This is done so that you can break up your project into however many files you’d like, and name them whatever you want, and nobody will tramp on each others’ toes. Think about it: if I have two gems installed, and both of them have their <code>lib</code> directories included, and they both name a file <code>json.rb</code>, which one is going to be loaded? It causes problems. So just follow this structure, and everything will work out.</p>

<p>The <code>version.rb</code> file looks like this:</p>

<div><span class="k">module</span> <span class="nn">Mygem</span>
  <span class="no">VERSION</span> <span class="o">=</span> <span class="s2">"0.0.1"</span>
<span class="k">end</span></div>

<p>This constant will be used in our gemspec. It’s nice to have it in a separate file, so that we can easily find and increment it when releasing a new version of the gem.</p>

<p>I’m sure that you can guess what goes in the <code>test</code> directory: your unit tests! You do have those, right? We’ll talk more about this later.</p>

<p>The <code>bin</code> directory holds any binaries that we want to distribute with our gem. Calling them ‘binaries’ is sort of a misnomer, though: these are almost always just Ruby scripts, starting off with a ‘shebang line’:</p>

<div><span class="c1">#!/usr/bin/env ruby</span>

<span class="k">begin</span>
  <span class="nb">require</span> <span class="s1">&#39;mygem&#39;</span>
<span class="k">rescue</span> <span class="no">LoadError</span>
  <span class="nb">require</span> <span class="s1">&#39;rubygems&#39;</span>
  <span class="nb">require</span> <span class="s1">&#39;mygem&#39;</span>
<span class="k">end</span>

<span class="c1">#more code goes here</span></div>

<p>This is a patten you’ll often see in gems that also give you scripts to run. Why the <code>begin</code>/<code>rescue</code>/<code>end</code>? Well, <a href="http://tomayko.com/writings/require-rubygems-antipattern">‘require “rubygems” is wrong’</a>. Basically, someone may be using something other than rubygems to manage their path, and so you shouldn’t trample on their toes. But if you can’t find your gem, then giving it a second shot with Rubygems is better than crashing.</p>

<h2 id="the_gemspec">The Gemspec</h2>

<p>Here’s a sample gemspec:</p>

<div><span class="c1"># -*- encoding: utf-8 -*-</span>
<span class="vg">$:</span><span class="p">.</span><span class="nf">push</span> <span class="no">File</span><span class="p">.</span><span class="nf">expand_path</span><span class="p">(</span><span class="s2">"../lib"</span><span class="p">,</span> <span class="kp">__FILE__</span><span class="p">)</span>
<span class="nb">require</span> <span class="s2">"mygem/version"</span>

<span class="no">Gem</span><span class="o">::</span><span class="no">Specification</span><span class="p">.</span><span class="nf">new</span> <span class="k">do</span> <span class="o">|</span><span class="n">s</span><span class="o">|</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">name</span>        <span class="o">=</span> <span class="s2">"mygem"</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">version</span>     <span class="o">=</span> <span class="no">Mygem</span><span class="o">::</span><span class="no">VERSION</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">platform</span>    <span class="o">=</span> <span class="no">Gem</span><span class="o">::</span><span class="no">Platform</span><span class="o">::</span><span class="no">RUBY</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">authors</span>     <span class="o">=</span> <span class="p">[</span><span class="s2">"Steve Klabnik"</span><span class="p">]</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">email</span>       <span class="o">=</span> <span class="p">[</span><span class="s2">"steve@steveklabnik.com"</span><span class="p">]</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">homepage</span>    <span class="o">=</span> <span class="s2">""</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">summary</span>     <span class="o">=</span> <span class="sx">%q{A sample gem}</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">description</span> <span class="o">=</span> <span class="sx">%q{A sample gem. It doesn&#39;t do a whole lot, but it&#39;s still useful.}</span>

  <span class="n">s</span><span class="p">.</span><span class="nf">add_runtime_dependency</span> <span class="s2">"launchy"</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">add_development_dependency</span> <span class="s2">"rspec"</span><span class="p">,</span> <span class="s2">"~&gt;2.5.0"</span>

  <span class="n">s</span><span class="p">.</span><span class="nf">files</span>         <span class="o">=</span> <span class="sb">`git ls-files`</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">test_files</span>    <span class="o">=</span> <span class="sb">`git ls-files -- {test,spec,features}/*`</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">executables</span>   <span class="o">=</span> <span class="sb">`git ls-files -- bin/*`</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">).</span><span class="nf">map</span><span class="p">{</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span> <span class="no">File</span><span class="p">.</span><span class="nf">basename</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="p">}</span>
  <span class="n">s</span><span class="p">.</span><span class="nf">require_paths</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"lib"</span><span class="p">]</span>
<span class="k">end</span></div>

<p>As you can see, it uses the standard ‘pass a block to new’ DSL convention to build up all of the information about our Gem. Most of it is pretty self-explanatory, but there are a few interesting parts: You can see we used the Mygem::VERSION constant we defined earlier to set our version. We use <code>git</code> to list all of the files in our project, as well as our test files and executables. The ‘add dependency’ lines tell <code>rubygems</code> what other gems we’ll need to install, if the user doesn’t have them already.</p>

<h2 id="tools_to_build_tools">Tools to build tools</h2>

<p>Because gems follow these conventions, there are a bunch of different gems that can help you make gems, like <a href="http://seattlerb.rubyforge.org/hoe/">hoe</a> or <a href="https://github.com/technicalpickles/jeweler">jeweler</a>. My favorite is a one-two punch with <code>rvm</code> and bundler.</p>

<p>If you’re not using <a href="http://rvm.beginrescueend.com/">rvm</a> already, you should. rvm is wonderful for a few reasons, but when you’re making a gem, rvm’s <a href="http://rvm.beginrescueend.com/gemsets/">gemsets</a> feature allow you to develop your gems in a cleanroom environment. This is nice for two reasons: you can verify that you have all of your dependencies configured properly, and you won’t pollute your regular Ruby setup with your undoubtedly half-broken under-development versions of your own gems.</p>

<p>Bundler is great for managing dependencies of gems in your applications, but it also includes two cool tools to help you make gems: <code>bundle gem &lt;gemname&gt;</code> will create a gem skeleton, and that skeleton is set up with a few <code>rake</code> tasks to make your development of gems nice and simple. The bundler skeleton sets up all of those directories I showed you above, as well as giving you a Gemfile, a <code>git</code> repository, and a .gitignore file. The three <code>rake</code> tasks bundler installs are ‘build,’ ‘install,’ and ‘release.’ ‘build’ builds your gem into a .gem, ‘install’ installs that gem into your current Ruby, and ‘release’ will tag your release, push it to GitHub, and then push your gem to rubygems.org. Super simple.</p>

<h2 id="an_example">An example</h2>

<p>I gave a lightning talk at <a href="http://pghrb.org/">pghrb</a> recently, and actually live-coded a gem while explaining this stuff. The resulting gem, which simply opens my presentation in a web browser, is on GitHub. It’s called <a href="https://github.com/steveklabnik/teachmehowtomakearubygem">teachmehowtomakearubygem</a>, and you can get it with <code>gem install teachmehowtomakearubygem</code>. I’m still revising the presentation to read better; I didn’t want to present a giant wall of text, but this article is much easier to read than the presentation is. Still, all of the example code is there.</p>

<h2 id="testing_your_gem">Testing your Gem</h2>

<p>If you set up your Rakefile to run your tests with <code>rake test</code>, you can take advantage of a really neat new project: <a href="http://gem-testers.org/">gem-testers</a>. The only other thing you need to do is add an empty <code>.gemtest</code> file to your project, and gem-testers will pick it up. Once enabled, your gem’s tests will be run on a variety of machines by a bunch of different people. This project is just getting underway, but similar efforts have provided a great benefit to people who write Perl libraries. Don’t have a Mac, but want to test your gem out on x86_64/darwin? gem-testers to the rescue!</p>

<h2 id="a_note_on_versioning">A note on versioning</h2>

<p>Try to follow <a href="http://semver.org/">semantic versioning</a> when releasing your gems. This makes it much easier for people using your gem to use things like ‘~&gt;’ when specifying the version they’d like to use, and not have to worry too much about API breakage. A little bit of work by everyone to follow conventions goes a long way.</p>

<h2 id="even_further_c_extensions">Even further: C extensions</h2>

<p>I don’t have much experience creating gems with C extensions, so if you have any best practices to share, please <a href="http://timeless.judofyr.net/comments">get in touch</a> and share them.</p>
]]></content>
  </entry>
  <entry>
    <title>On Camping vs Sinatra</title>
    <id>tag:timeless.judofyr.net,2011-01-30:1296423381</id>
    <link href="http://timelessrepo.com/on-camping-vs-sinatra"/>
    <updated>2011-01-30T21:36:21Z</updated>
    <published>2011-01-30T21:36:21Z</published>
    <content type="html"><![CDATA[
<p>Many years ago there was a tiny little <strike>ladybug</strike> Ruby web framework called <a href="http://whywentcamping.com/">Camping</a>. Compared Rails’ thousands of lines of code, camping.rb was a tempting alternative for simple applications:</p>
<center><img src='https://github.com/camping/camping/raw/master/extras/images/little-wheels.png' /></center><hr />
<p>Welcome to the present, where every test framework ships with its own micro framework. Or was it the other way? Anyway, my point was: With so many alternatives, why would you want to go Camping? Why not Sinatra?</p>

<p>Some time ago, this discussion started over at the <a href="http://hackety-hack.com/">Hackety Hack</a> mailing list, so I decided to chime in and present <strong><em>SIX (UNIMPRESSIVE) REASONS CAMPING IS BETTER THAN YOU WOULD IMAGINE</em></strong>.</p>

<p>Please don’t mistake me, this is not <strong><em>SIX (UNIMPRESSIVE) REASONS CAMPING IS BETTER THAN SINATRA</em></strong> or even <strong><em>SIX (UNIMPRESSIVE) REASONS YOU SHOULD DROP EVERYTHING YOU HAVE IN YOUR HAND RIGHT NOW AND START USING CAMPING</em></strong>. All I’m saying is that Camping gets so many things <em>right</em>. Not necessarily in very few lines of code or very fast, but nonetheless: I look at Camping code and nod to myself: “Yeah, this is probably the <em>correct</em> way to do it”.</p>

<p>Of course, this doesn’t really matter. If we cared about correctness, we would program in Haskell, not some language where monkey patching is acceptable in production code. As long as you’re comfortable in Sinatra (or any other framework), you should continue doing that.</p>

<p>Without any more ado:</p>

<pre><code>#!/usr/bin/ruby
SIX (UNIMPRESSIVE)                 # Markdown version:
REASONS CAMPING IS BETTER          # 1) Download http://timeless.judofyr.net/camping-vs-sinatra.rb
THAN YOU WOULD IMAGINE             # 2) ruby camping-vs-sinatra.rb

reasons.push(COMMUNITY) do %%
  Yes, Sinatra has a big community, but Camping definitely has a great
  community too. Size doesn&#39;t always matter. Because there are so few users,
  it means every single issue gets full attention.

  If you&#39;re only looking at the GitHub repo or ruby.reddit.com, Camping
  might seem a little dead, but that&#39;s because everything happens on the
  mailing lists so it&#39;s not always visible from the &quot;outside&quot;. (And other
  times, we&#39;re simply restin&#39;, just waiting for a question, suggestion or
  comment.)

  Besides, I don&#39;t allow Camping to disappear. Not because I need it in my
  business or something like that, but because the code is so fucking great.
  I simply won&#39;t allow it to die. Therefore I will *always* do my best to
  help people who are camping (just ask Eric Mill on this mailing list).
%%%%% end

reasons.push(UNPOLLUTED) do %%
  In Sinatra it&#39;s a norm (whether you use Sinatra::Base or not), in Camping
  it&#39;s the law:

      Camping.goes :Blog
      module Blog; end

      Camping.goes :Wiki
      module Wiki; end

  Every application lives under its own namespace. Yes, it requires a few
  more characters, but when you think about it, why *should* we allow are
  apps to run directly under the global namespace? That&#39;s surely not how we
  design our other Ruby code. What makes it so different? Shouldn&#39;t you for
  instance be able to `require &quot;app&quot;` and `include App::Helpers` somewhere
  else?

  Think of the environment; reduce your pollution!
%%%%% end

reasons.push(RESTful) do %%
  A central idea in REST is the concept of a resource, and that you can call
  methods on the resource (in order to get a representation of it). How would
  you apply these ideas in Ruby? What about this?

      class Posts
        def get; end
        def post; end
      end

  I would say this fits the description perfectly. You can instantiate
  instances of this class (with different parameters etc.) for each request,
  and then call methods on it. Guess how it looks in Camping?

      module App::Controllers
        class Posts
          def get; end
          def post; end
        end
      end

  The best part: Camping doesn&#39;t care if you use GET, DELETE, PROPFIND or
  HELLOWORD; every method is threated equally. One of the early ideas of HTTP
  was that you could easily extend it with your own methods for your own
  needs, and Camping is a perfect match for these cases!
%%%%% end

reasons.push(RUBY) do %%
  Ruby has wonderful features such as classes, inheritance, modules and
  methods. Why should every single DSL replace these features by blocks?
  Often, all they do is to hide details, without improving anything else than
  line count. Let me show you an example:

      get &#39;/posts&#39; do
        # code
      end

  Now answer me:

  1. Where is this code stored?
  2. How do I override the code?
  3. What happens if I call `get &#39;/posts&#39;` again?

  Not quite sure? Let&#39;s have a look at Camping:

      module App::Controllers
        class Posts
          def get
            # code
          end
        end
      end

  Since this is just &quot;plain&quot; Ruby, it&#39;s much simpler:

  ### 1. Where is this code stored?

  The code is stored as a method, and we can easily play with it:

      Posts.instance_methods(false) # =&gt; [:get]
      Posts.instance_method(:get)   # =&gt; #&lt;UnboundMethod: Posts#get&gt;
      # Given post.is_a?(Posts)
      post.methods(false)           # =&gt; [:get]
      post.method(:get)             # =&gt; #&lt;Method: Posts#get&gt;

  ### 2. How do I override the code?

  Just like you would override a method:

      class App::Controllers::Posts
        def get
          # override
        end
      end

      # or, if post.is_a?(Posts)

      def post.get
        # override
      end

  ### 3. What happens if I call `class Posts` again?

  Because Ruby has open classes, we know that it would have no effect at all.</code></pre>
<hr />
<pre><code>  Another advantage of having resources as classes (and not as blocks):

      module IUseTheseMethodsALot
        def get; end
      end

      module App::Controllers
        class Posts
          include IUseTheseMethodsALot
        end

        class Users
          include IUseTheseMethodsALot
        end
      end
%%%%% end

reasons.push(NAMING) do %q%
  In Camping you&#39;ll have to give every resource a name, while in Sinatra
  they&#39;re always anonymous. By giving resources a name you have a way of
  referencing them, which can be very convenient:

      post &#39;/issue&#39; do
        issue = Issue.create(params[:issue])
        redirect &quot;/issue/#{issue.id}/overview&quot;
      end

  Since every resource is anonymous in Sinatra, you&#39;re forced to hard-code
  the path. Not very elegant, and it can be a pain to update the code if you
  for instance want to move all urls from issue/ to i/. Camping&#39;s solution:

      class Issue
        def post
          issue = Issue.create(@input.issue)
          redirect R(IssueOverview, issue)
        end
      end

  The R method in Ruby returns the URL to a resource (which takes one
  parameter). Camping automatically calls #to_param on the arguments, so you
  can safely pass in ActiveRecord objects too. If you want to change the
  route to IssueOverview, you can do this in *one* place and you&#39;re done.
%%%%% end

reasons.push(RELOADING) do %%
      $ camping app.rb
      ** Starting Mongrel on 0.0.0.0:3301

  The Camping Server provides code reloading (so you don&#39;t need to restart
  the server while you develop your app) that works out of the box on *all*
  platforms (including Windows). We actually care about our Windows users!
%%%%% end
                                                                        &#39;&#39;
                                                                        &#39;&#39;
BEGIN {def Object.const_missing(m);m.to_s end;def method_missing(*a)a[1]=
$h.pop if a[1]==$h;$h.push(a) end;$h = [];def reasons; $reas ||= {};end;&#39;&#39;
def reasons.push(r,&amp;b);self[r]=b.call;end;END {puts h=$h*&#39; &#39;,&#39;=&#39;*h.size,&#39;&#39;
reasons.each { |name, val| puts name, &#39;-&#39;*name.size, val.gsub(/^  /,&#39;&#39;),&#39;&#39;
                                                                        &#39;&#39;
}}}  # Please keep all my mustaches intact.   // Magnus Holm</code></pre>
]]></content>
  </entry>
  <entry>
    <title>Haters gonna HATEOAS</title>
    <id>tag:timeless.judofyr.net,2010-01-23:1264255523</id>
    <link href="http://timelessrepo.com/haters-gonna-hateoas"/>
    <updated>2010-01-23T14:05:23Z</updated>
    <published>2010-01-23T14:05:23Z</published>
    <content type="html"><![CDATA[
<p>Every time someone mentions RESTful web services, there’s always that one person that has to chime in: “That’s not <em>really</em> RESTful, it’s just kinda RESTful.” I’d always filed that information away, under ‘things to learn later,’ and let it simmer in the back of my brain. I’ve finally looked into it, and they’re absolutely right: 99.99% of the RESTful APIs out there aren’t fully compliant with Roy Fielding’s conception of REST. Is that bad?</p>

<p>Before we answer that question, let’s back up a bit: Why aren’t these web services RESTful? Just what is REST, anyway? REST was created by Roy Fielding in <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">his dissertation</a> if you’d like the full lowdown, but we’re more concerned with RESTful API design than we are in the full system. A more useful framework for this discussion is the <a href="http://martinfowler.com/articles/richardsonMaturityModel.html">Richardson Maturity Model</a>. Basically, it defines four possible levels of ‘REST support’:</p>

<ol>
<li>“The Swamp of POX.” You’re using HTTP to make RPC calls. HTTP is only really used as a tunnel.</li>

<li>Resources. Rather than making every call to a service endpoint, you have multiple endpoints that are used to represent resources, and you’re talking to them. This is the very beginnings of supporting REST.</li>

<li>HTTP Verbs. This is the level that something like Rails gives you out of the box: You interact with these Resources using HTTP verbs, rather than always using POST.</li>

<li>Hypermedia Controls. HATEOAS. You’re 100% REST compliant.</li>
</ol>

<h2 id="the_four_levels_of_rest">The four levels of REST</h2>

<p>Let’s start at the bottom and work our way up.</p>

<p>Now, The Swamp of POX means that you’re using HTTP. Technically, REST services can be provided over any application layer protocol as long as they conform to certain properties. In practice, basically everyone uses HTTP. And since we’re discussing the creation of an API that conforms to REST rather than a system architecture based on the principles of REST, HTTP is a solid assumption on our part.</p>

<p>Level one is where it starts to get interesting. REST’s ‘resources’ are the core pieces of data that your application acts on. These will often correspond to the Models in your application, if you’re following MVC. A blog, for example, would have Entry resources and Comment resources. API design at Level 1 is all about using different URLs to interact with the different resources in your application. To make a new Entry, you’d use <code>/entries/make_new</code>, but with comments, it’d be <code>/comments/make_new</code>. So far, so good. This stuff is easy.</p>

<p>However, there are a set of common operations that are performed on resources, and it seems kinda silly to make a new URI for every operation, especially when they’re shared. That’s where Level 2 comes in. We’re always going to need to perform CRUD operations on our resources, so why not find a way to share these operations across resources? We accomplish this using HTTP Verbs. If we want to get a list of Entries, we make a GET request to <code>/entries</code>, but if we want to create a new Entry, we <code>POST</code> rather than GET. Pretty simple.</p>

<p>The final level, Hypermedia Controls, is the one that everyone falls down on. There’s two parts to this: content negotiation and HATEOAS. Content negotiation is focused on different representations of a particular resource, and HATEOAS is about the discoverability of actions on a resource.</p>

<h2 id="content_negotiation">Content Negotiation</h2>

<p>At its simplest, this is something that Rails does right, too. Check out these lines from running <code>rails scaffold</code>:</p>

<div><span class="k">def</span> <span class="nf">index</span>
  <span class="vi">@entries</span> <span class="o">=</span> <span class="no">Entry</span><span class="p">.</span><span class="nf">all</span>
  <span class="n">respond_to</span> <span class="k">do</span> <span class="o">|</span><span class="nb">format</span><span class="o">|</span>
    <span class="nb">format</span><span class="p">.</span><span class="nf">html</span>
    <span class="nb">format</span><span class="p">.</span><span class="nf">xml</span> <span class="p">{</span> <span class="n">render</span> <span class="ss">:xml</span> <span class="o">=&gt;</span> <span class="vi">@entries</span> <span class="p">}</span>
    <span class="nb">format</span><span class="p">.</span><span class="nf">json</span> <span class="p">{</span> <span class="n">render</span> <span class="ss">:json</span> <span class="o">=&gt;</span> <span class="vi">@entries</span> <span class="p">}</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>This is content negotiation. You’re able to use MIME types to request a representation of a resource in different formats. Rails 3 has made this even better, with <code>respond_to</code>/<code>respond_with</code>:</p>

<div><span class="k">class</span> <span class="nc">EntriesController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span><span class="o">::</span><span class="no">Base</span>

  <span class="n">respond_to</span> <span class="ss">:html</span><span class="p">,</span> <span class="ss">:xml</span><span class="p">,</span> <span class="ss">:json</span>

  <span class="k">def</span> <span class="nf">index</span>
    <span class="n">respond_with</span><span class="p">(</span><span class="vi">@entries</span> <span class="o">=</span> <span class="no">Entry</span><span class="p">.</span><span class="nf">all</span><span class="p">)</span>
  <span class="k">end</span></div>

<p>Super simple. So why do I say people get this wrong? Well, this is a great usage of content negotiation, but there’s also one that almost everyone gets wrong. Content negotiation is the answer to the question, “How do I version my API?” The proper way to do this is with the <code>Accepts</code> header, and use a MIME type like <code>application/yourcompany.v1+json</code>. There’s a great article about this by Peter Williams <a href="http://barelyenough.org/blog/2008/05/versioning-rest-web-services/">here</a>.</p>

<h2 id="hateoas">HATEOAS</h2>

<p>The last constraint is incredibly simple, but nobody actually does it. It’s named Hypertext As The Engine Of Application State. I still haven’t decided how to pronounce the acronym, I always try to say “Hate ee ohs,” which sounds like a breakfast cereal. Anyway, let’s break this down. We’re using Hypertext, fine, that makes sense. But what’s it mean to be an engine? And application state?</p>

<p>It’s all about state transitions. It’s right there in the name: Representational State Transfer. Your application is just a big state machine.</p>

<p><img src="http://i.imgur.com/9E28g.gif" alt="fsm" /></p>

<p>Your APIs should do this. There should be a single endpoint for the resource, and all of the other actions you’d need to undertake should be able to be discovered by inspecting that resource. Here’s an example of what our <code>Entry</code> XML might look like if Rails handled HATEOAS:</p>

<pre><code>&lt;entry&gt;
  &lt;id&gt;1337&lt;/id&gt;
  &lt;author&gt;Steve Klabnik&lt;/author&gt;
  &lt;link rel = &quot;/linkrels/entry/newcomment&quot;
        uri = &quot;/entries/1337/comments&quot; /&gt;
  &lt;link rel = &quot;self&quot;
        uri = &quot;/entries/1337&quot; /&gt;
&lt;/entry&gt;</code></pre>

<p>When we GET a particular Entry, we discover where we can go next: we can make a new comment. It’s a discoverable action. The particular state we’re in shows what other states we can reach from here.</p>

<p>Now, when I said ‘nobody’ does this, what I meant was ‘for APIs.’ This is exactly how the Web works. Think about it. You start off on the homepage. That’s the only URL you have to know. From there, a bunch of links point you towards each state that you can reach from there. People would consider it ludicrous if they had to remember a dozen URLs to navigate a website, so why do we expect the consumers of our APIs to do so as well?</p>

<p>There’s another benefit here as well: we’ve decoupled the URL itself from the action we’re having it perform. Think about it like the web: If we have a link that says “click here to make a new blog entry” and next week, we change it from <code>/entries/new</code> to <code>/somethingelse/whatever</code>, users of the web site (probably) won’t notice: they’re just clicking on the link that takes them where they need to go. If you changed the text to “click here to do something else” they wouldn’t expect it to be making an Entry anymore. By the same token, we can change the URI in our <code>&lt;link&gt;</code> tag, and a proper client will just automatically follow along. Brilliant!</p>

<h2 id="why_arent_we_doing_this_already">Why aren’t we doing this already?</h2>

<p>Well, for one thing, the tooling just isn’t there to do this. Think of how web development was before Rails started emphasizing REST: some people got it right, but not many people cared. I know that I had teachers telling me that a <code>&lt;form&gt;</code> with <code>method=&quot;GET&quot;</code> was perfectly fine, and that the only real difference between <code>GET</code> and <code>POST</code> is if the parameters are in the URL… but I digress. Until we make this kind of development easy to do, people aren’t going to do it. There’s also a serious lack of education on this topic. The web development community has been steadily improving as the web grows and matures, and so I hope that eventually we’ll see more people actually adopting HATEOAS and going ‘full RESTful.’</p>

<p>There’s also some discussion about how useful extra constraints actually are. If this is such a big important deal, why aren’t more people doing it? I haven’t yet implemented a 100% RESTful API myself yet, so I can’t really say. I do believe that I’ll be giving it a shot in the future, and I think that as our collective understanding of Fielding’s work improves, we’ll eventually see the value in REST.</p>
]]></content>
  </entry>
  <entry>
    <title>Your blog is a project</title>
    <id>tag:timeless.judofyr.net,2011-01-17:1295296503</id>
    <link href="http://timelessrepo.com/your-blog-is-a-project"/>
    <updated>2011-01-17T20:35:03Z</updated>
    <published>2011-01-17T20:35:03Z</published>
    <content type="html"><![CDATA[
<p>Maintaining a blog is surprisingly more difficult than you would imagine. The concept is so simple that anyone instantly understands it. The software has become so easy to work with that anyone can start one in a few minutes. <em>Starting</em> a blog has never been easier, yet keeping it updated still remains a most demanding task.</p>

<p>The truth is that blogging is often more about structuring and organizing than actual <em>writing</em>. Well, guess what? Successful software development is <em>also</em> often more about structuring and organizing than actual writing, so why not apply the same tools to your blog as to any other project?</p>

<h2 id="deadlines_or_learn_to_ship">Deadlines, or: Learn to ship</h2>

<p>Who doesn’t hate deadlines? Who doesn’t want to continually work on something until it’s absolutely perfect and then release it to the world? Deadlines are often despised because estimation is never perfect and nobody enjoys pressure, but truth is that pressure is often <em>required</em> to get anything done. Especially when it comes to situations where no one will complain if you don’t get anything done (a new blog, startup or open-source project).</p>

<p>Don’t say “I’m going to blog when I have something interesting to tell”, say “I’m going to blog something each Sunday”. The implication of this is that you’ll have to <strong>find something interesting to tell every week</strong>. Of course, this is only a soft deadline: there will be no real consequences for <em>not</em> holding the deadline, but it will turn your blog from a passive project into an active project which brings it into a whole new level.</p>

<p>Another consequence of setting deadlines (and sticking to them) is that you’ll force yourself to ship. It’s so tempting to try to turn every blog post into a masterpiece, but far to often you get a better result by simply publishing and taking feedback from there.</p>

<p>Force yourself out of your comfort zone and start <em>publishing!</em></p>

<blockquote>
<p>Move out of your comfort zone. You can only grow if you are willing to feel awkward and uncomfortable when you try something new.</p>

<p>— Brian Tracy</p>
</blockquote>

<h2 id="issue_tracker_or_nobodys_perfect">Issue tracker, or: Nobody’s perfect</h2>

<p>Software is rarely perfect, and in order to fix bugs you must first identify and organize them. Issue trackers are an important part of any project, so why not use it for your blog too? You just got an idea for something to write about? Sounds like a feature request to me. The RSS feed isn’t working correctly? Sounds like a bug. Don’t just make mental notes about your blog, treat it right a proper project.</p>

<p>Here at Timeless we have <a href="http://github.com/judofyr/timeless/issues">an issue tracker</a> with tons of “reported” issues. It’s a simple place where I can quickly scribble down new ideas or just organize known “bugs”. Every time I go into “blog mode” I take a look at that huge list, and I always discover something new. For instance, I had no idea I was going to write this article until I peeked at the issue list and thought: “Yep, today I want to write about how you should treat your blog.”</p>

<p>An issue tracker is no different than a simple to-do list, so feel free to use whatever suits you best, but the important part is to actually <strong>write things down</strong> and don’t keep everything in your mind.</p>

<h2 id="but_wait_theres_more">But wait, there’s more!</h2>

<p>Not really. There isn’t more in this article yet. Why? Because I’ve already passed my deadline and I <em>really</em> need to get this article published. Instead of listening to the perfectionist inside of me, I’m just going to ship this little piece of text and rather work on it later.</p>

<p>As always, please <a href="/comments">let me know</a> what you think about this idea.</p>
]]></content>
  </entry>
  <entry>
    <title>Literate Programming</title>
    <id>tag:timeless.judofyr.net,2011-01-10:1294698403</id>
    <link href="http://timelessrepo.com/literate-programming.html"/>
    <updated>2011-01-10T22:26:43Z</updated>
    <published>2011-01-10T22:26:43Z</published>
    <content type="html"><![CDATA[
<p><em>Literate programming</em> is an interesting concept that Don Knuth described in 1984, as an alternate way to write software. Literate programming places emphasis on the way that people think about software, rather than the way that computers do. Rather than write code in an order that’s imposed by the compiler, the literate programmer writes in words the way he expects a program to work, and then intersperses his description with code.</p>
<p><a href="http://timelessrepo.com/literate-programming.html">Continue to full post.</a></p>]]></content>
  </entry>
  <entry>
    <title>Mockup-Driven Development</title>
    <id>tag:timeless.judofyr.net,2011-01-02:1294007203</id>
    <link href="http://timelessrepo.com/mockup-driven-development"/>
    <updated>2011-01-02T22:26:43Z</updated>
    <published>2011-01-02T22:26:43Z</published>
    <content type="html"><![CDATA[
<p>Design in web development is usually accomplished in three main steps:</p>

<ol>
<li>Concept</li>

<li>Mockup</li>

<li>Template</li>
</ol>

<p>Mockup-Driven Development attempts to merge the last two in order to make it easier to <strong>build</strong>, <strong>maintain</strong> and <strong>experiment</strong> with your application. Let’s have a look at some of the real issues present in today’s workflow and how MDD tries to solve them. We’ll also check out the current implementations, what issues they have and how we can improve them.</p>

<h2 id="the_steps">The Steps</h2>

<h3 id="step_1_concept">Step 1: Concept</h3>

<p>I’m not going to focus on the first step at all, so let’s just define it as <em>everything that is done before you have a mockup in HTML</em>. That includes sketches, Photoshop mockups, and so on.</p>

<h3 id="step_2_mockup_in_html">Step 2: Mockup (in HTML)</h3>

<p>The next step is to create some HTML so you can actually see how the site would end up. It’s all static files and nothing is functional, but you can at least get a feeling about what works and what you need to improve on. More importantly, mockups are a direct step on the path to a functional application: This is actually a concrete product which will be used later on.</p>

<h3 id="step_3_template">Step 3: Template</h3>

<p>The last step is to integrate the mockups with the rest of the application in order to Make Things Work™. Usually this involves annotating the HTML where the dynamic data should be inserted. I like to think about it as punching out “holes” which you’ll fill out with data at every request.</p>

<h2 id="the_workflows">The Workflows</h2>

<p>Most web application will, at least to some extent, go through these three steps and in order to get the best result you want to have the most skilled people working at each step: A <em>designer</em> works on the concept, a <em>web designer</em> knows how to turn it into a mockup and a <em>developer</em> makes it work.</p>

<p>I’ll focus mostly on the workflow between the mockups and the templates.</p>

<h3 id="separate_mockups_and_templates">Separate mockups and templates</h3>

<p>One workflow is that the web designer <em>only</em> works with the mockups while the developer <em>only</em> works with the templates. Every time there is a change in the mockups (or the templates for that matter), you’ll have to remember to keep the two sets of files in sync.</p>

<p>There’s one big advantage of this workflow: The web designer can continue working on the application in the <em>exactly</em> same way throughout the process. The great thing about this process is that <strong>it’s only static files</strong>: You can easily view them in <em>any</em> browser on <em>any</em> computer and you don’t need to have the application running in order to view the final result.</p>

<p>A disadvantage with this approach is that mockups are <strong>horrible to maintain</strong>. There’s no layouts or partials, so if the web designer wants to change something that’s shared among all the pages, he’ll have to update <em>all</em> the pages. You might use something like Dreamweaver’s template functionality to solve this issue, but now you’ve locked everyone into using a single tool.</p>

<p>And of course, now that you have two sets of the design you’ll constantly need to <strong>keep them in sync</strong>. Every change in the mockup needs to be reflected in the template (and the other way around). This means that while it’s super easy to experiment at each of the steps, you’ll end up with a massive merging process at the end of each iteration.</p>

<h3 id="everyone_works_on_the_template">Everyone works on the template</h3>

<p>Because of the disadvantages of the approach above, most people have a little different workflow. This is for instance the way <a href="http://www.loudthinking.com/arc/000405.html">37signals</a> works:</p>

<blockquote>
<p>This is how we work:</p>

<ol>
<li>Designer cooks up static HTML template.</li>

<li>Programmer sprinkle it with ERB tags to make the dynamic elements dynamic.</li>

<li>Designer and programmer can both revisit the template to make changes.</li>
</ol>
</blockquote>

<p>After the initial mockup (which you turn into a template), you just forget about it and everyone rather works directly on the template. This fixes two of the issues in the previous approach: There’s only a single set of files <em>and</em> you can use your template engine’s features to deal with layouts, partials etc.</p>

<p>The main disadvantage is that you know <strong>need to have the application running</strong> to see the result; you can no longer open the files in your browser. Depending on the complexity of your application and the qualifications of your team, this may or may not be an issue for you. If all your web designers know how to code, there’s not really a problem here. If you have a very simple application, you can probably teach most people how to run it. If you on the other hand have a quite complex application with many moving parts, you’ll need to do a lot of work to make it easily runnable for everyone.</p>

<h2 id="mockupdriven_development">Mockup-Driven Development</h2>

<p>The concept behind Mockup-Driven Development is essentially to combine the concept of a mockup and a template. The optimal workflow in MDD goes something like this:</p>

<ol>
<li>Designer cooks up static HTML mockup.</li>

<li>Programmer annotates the mockup so the template engine understands it.</li>

<li>Designer can still make changes to the mockup and view it in the browser.</li>
</ol>

<p>MDD boils down to <strong>three essential features</strong>:</p>

<ul>
<li>The template must be a static file which can be viewed in a browser.</li>

<li>The template must also be renderable by the template engine.</li>

<li>The template should be easy to maintain (e.g. no duplication).</li>
</ul>

<p>Let’s have a look at two template engines that’s built upon MDD to easier understand what we’re <em>really</em> talking about.</p>

<h2 id="implementations">Implementations</h2>

<h3 id="lilu_and_effigy">Lilu and Effigy</h3>

<p>Lilu (written by <a href="https://twitter.com/yrashk">Yurii Rashkovskii</a>) was the first mockup-based template engine I discovered. It’s not being actively developed anymore, but luckily there’s <a href="https://github.com/jferris/effigy">Effigy</a>, another project which is essentially a more modern version of Lilu. The concept behind them is the same.</p>

<p>You start off with a plain HTML mockup:</p>

<div><span class="nt">&lt;html&gt;</span>
  <span class="nt">&lt;head&gt;</span>
    <span class="nt">&lt;title&gt;&lt;/title&gt;</span>
  <span class="nt">&lt;/head&gt;</span>
  <span class="nt">&lt;body&gt;</span>
    <span class="nt">&lt;h1&gt;</span>Post Title<span class="nt">&lt;/h1&gt;</span>
    <span class="nt">&lt;p</span> <span class="na">class=</span><span class="s">"body"</span><span class="nt">&gt;</span>Content of the blog<span class="nt">&lt;/p&gt;</span>
    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"comment"</span><span class="nt">&gt;</span>
      <span class="nt">&lt;h2&gt;</span>Comment Title<span class="nt">&lt;/h2&gt;</span>
      <span class="nt">&lt;p&gt;</span>Content of the comment<span class="nt">&lt;/p&gt;</span>
      <span class="nt">&lt;a&gt;</span>View more<span class="nt">&lt;/a&gt;</span>
    <span class="nt">&lt;/div&gt;</span>
    <span class="nt">&lt;p</span> <span class="na">id=</span><span class="s">"no-comments"</span><span class="nt">&gt;</span>There aren&#39;t any comments for this post.<span class="nt">&lt;/p&gt;</span>
  <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span></div>

<p>Then you write some glue code in Ruby:</p>

<div><span class="k">class</span> <span class="nc">PostView</span> <span class="o">&lt;</span> <span class="no">Effigy</span><span class="o">::</span><span class="no">View</span>
  <span class="nb">attr_reader</span> <span class="ss">:post</span>

  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">post</span><span class="p">)</span>
    <span class="vi">@post</span> <span class="o">=</span> <span class="n">post</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">transform</span>
    <span class="n">text</span><span class="p">(</span><span class="s1">&#39;h1&#39;</span><span class="p">,</span> <span class="n">post</span><span class="p">.</span><span class="nf">title</span><span class="p">)</span>
    <span class="n">text</span><span class="p">(</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s2">"</span><span class="si">#{</span><span class="n">post</span><span class="p">.</span><span class="nf">title</span><span class="si">}</span><span class="s2"> - Site title"</span><span class="p">)</span>
    <span class="n">text</span><span class="p">(</span><span class="s1">&#39;p.body&#39;</span><span class="p">,</span> <span class="n">post</span><span class="p">.</span><span class="nf">body</span><span class="p">)</span>
    <span class="n">replace_each</span><span class="p">(</span><span class="s1">&#39;.comment&#39;</span><span class="p">,</span> <span class="n">post</span><span class="p">.</span><span class="nf">comments</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">comment</span><span class="o">|</span>
      <span class="n">text</span><span class="p">(</span><span class="s1">&#39;h2&#39;</span><span class="p">,</span> <span class="n">comment</span><span class="p">.</span><span class="nf">title</span><span class="p">)</span>
      <span class="n">text</span><span class="p">(</span><span class="s1">&#39;p&#39;</span><span class="p">,</span> <span class="n">comment</span><span class="p">.</span><span class="nf">summary</span><span class="p">)</span>
      <span class="kp">attr</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">,</span> <span class="ss">:href</span> <span class="o">=&gt;</span> <span class="n">url_for</span><span class="p">(</span><span class="n">comment</span><span class="p">))</span>
    <span class="k">end</span>
    <span class="n">remove</span><span class="p">(</span><span class="s1">&#39;#no-comments&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="n">post</span><span class="p">.</span><span class="nf">comments</span><span class="p">.</span><span class="nf">empty?</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>This approach makes your templates very designer friendly (there’s no “weird stuff”), at the expense of a more loosely connection between the template and the data. There’s nothing in the template which tells the web designer what is dynamic data, so he might break the page if he for instance changes the comment titles to use H3 tags instead.</p>

<h3 id="ruhl">RuHL</h3>

<p><a href="http://docs.stonean.com/page/ruhl">RuHL</a> is “an opinionated attribute language” written by <a href="http://stonean.com/">Andrew Stone</a>. By placing the dynamic parts into a <code>data-ruhl</code> attribute (as shown below), we have made it explicit which parts are dynamic. As long as the web designer preserves these annotations, he can freely move everything around without breaking the page:</p>

<div><span class="nt">&lt;html&gt;</span>
  <span class="nt">&lt;head&gt;</span>
    <span class="nt">&lt;title</span> <span class="na">data-ruhl=</span><span class="s">"title"</span><span class="nt">&gt;&lt;/title&gt;</span>
  <span class="nt">&lt;/head&gt;</span>
  <span class="nt">&lt;body&gt;</span>
    <span class="nt">&lt;h1</span> <span class="na">data-ruhl=</span><span class="s">"title"</span><span class="nt">&gt;</span>Post Title<span class="nt">&lt;/h1&gt;</span>
    <span class="nt">&lt;p</span> <span class="na">class=</span><span class="s">"body"</span> <span class="na">data-ruhl=</span><span class="s">"body"</span><span class="nt">&gt;</span>Content of the blog<span class="nt">&lt;/p&gt;</span>
    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"comment"</span> <span class="na">data-ruhl=</span><span class="s">"_use: comments"</span><span class="nt">&gt;</span>
      <span class="nt">&lt;h2</span> <span class="na">data-ruhl=</span><span class="s">"title"</span><span class="nt">&gt;</span>Comment Title<span class="nt">&lt;/h2&gt;</span>
      <span class="nt">&lt;p</span> <span class="na">data-ruhl=</span><span class="s">"comment"</span><span class="nt">&gt;</span>Content of the comment<span class="nt">&lt;/p&gt;</span>
      <span class="nt">&lt;a&gt;</span>View more<span class="nt">&lt;/a&gt;</span>
    <span class="nt">&lt;/div&gt;</span>
    <span class="nt">&lt;p</span> <span class="na">id=</span><span class="s">"no-comments"</span> <span class="na">data-ruhl=</span><span class="s">"_unless: comments"</span><span class="nt">&gt;</span>There aren&#39;t any comments for this post.<span class="nt">&lt;/p&gt;</span>
  <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span></div>

<h3 id="a_few_issues">A few issues…</h3>

<p>Initially, the examples above may seem to follow MDD, but there’s a few issues that makes them less convincing. First of all, <strong>there are no layouts or partials</strong>. Unless you want to repeat yourself in the templates, you’ll have to handle this in code inside your application.</p>

<p>For instance, in Effigy we might do something like:</p>

<div><span class="k">class</span> <span class="nc">Layout</span> <span class="o">&lt;</span> <span class="no">Effigy</span><span class="o">::</span><span class="no">View</span>
  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="n">template</span><span class="p">)</span>
    <span class="vi">@view</span> <span class="o">=</span> <span class="n">view</span>
    <span class="vi">@template</span> <span class="o">=</span> <span class="n">template</span>
  <span class="k">end</span>
  
  <span class="k">def</span> <span class="nf">transform</span>
    <span class="n">html</span><span class="p">(</span><span class="s1">&#39;#content&#39;</span><span class="p">,</span> <span class="vi">@view</span><span class="p">.</span><span class="nf">render_html_document</span><span class="p">(</span><span class="vi">@template</span><span class="p">))</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>And suddenly we’ve broken one rule of MDD: You should be able view the file in the browser. If you open a template file which depends on the layout, you will only see the template and not the layout (no CSS or JavaScript for you, baby).</p>

<p>Also, if we take another look at the Effigy mockup, we’ll notice that something isn’t quite right:</p>

<div><span class="nt">&lt;html&gt;</span>
  <span class="nt">&lt;head&gt;</span>
    <span class="nt">&lt;title&gt;&lt;/title&gt;</span>
  <span class="nt">&lt;/head&gt;</span>
  <span class="nt">&lt;body&gt;</span>
    <span class="nt">&lt;h1&gt;</span>Post Title<span class="nt">&lt;/h1&gt;</span>
    <span class="nt">&lt;p</span> <span class="na">class=</span><span class="s">"body"</span><span class="nt">&gt;</span>Content of the blog<span class="nt">&lt;/p&gt;</span>
    <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"comment"</span><span class="nt">&gt;</span>
      <span class="nt">&lt;h2&gt;</span>Comment Title<span class="nt">&lt;/h2&gt;</span>
      <span class="nt">&lt;p&gt;</span>Content of the comment<span class="nt">&lt;/p&gt;</span>
      <span class="nt">&lt;a&gt;</span>View more<span class="nt">&lt;/a&gt;</span>
    <span class="nt">&lt;/div&gt;</span>
    <span class="nt">&lt;p</span> <span class="na">id=</span><span class="s">"no-comments"</span><span class="nt">&gt;</span>There aren&#39;t any comments for this post.<span class="nt">&lt;/p&gt;</span>
  <span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span></div>

<p>When the application is running, the actual comments will <em>never</em> be displayed together with the “No comments” message, yet that’s exactly what happens in Effigy and RuHL. In this specific case it might not be so important, but there’s a deeper failure here: <strong>The mockup doesn’t actually present the actual application.</strong> Ouch.</p>

<h2 id="javascript_to_the_rescue">JavaScript to the rescue!</h2>

<p>Let me show you two hypothetical templates:</p>

<p>layout.html:</p>

<div><span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
  <span class="nt">&lt;title&gt;&lt;/title&gt;</span>
  <span class="nt">&lt;script </span><span class="na">src=</span><span class="s">"mockle.js"</span><span class="nt">&gt;&lt;/script&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
  <span class="nt">&lt;mockle</span> <span class="na">yield=</span><span class="s">"content"</span><span class="nt">&gt;&lt;/mockle&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span></div>

<p>post.html:</p>

<div><span class="nt">&lt;script </span><span class="na">src=</span><span class="s">"mockle.js"</span><span class="nt">&gt;</span>
  <span class="p">{</span> <span class="s2">"layout"</span><span class="p">:</span> <span class="s2">"layout.html"</span> <span class="p">}</span>
<span class="nt">&lt;/script&gt;</span>

<span class="nt">&lt;h1&gt;</span>Post Title<span class="nt">&lt;/h1&gt;</span>
<span class="nt">&lt;p</span> <span class="na">class=</span><span class="s">"body"</span><span class="nt">&gt;</span>Content of the blog<span class="nt">&lt;/p&gt;</span>

<span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">"comments"</span> <span class="na">mockle:scenario=</span><span class="s">"comments: Many"</span><span class="nt">&gt;</span>
  <span class="nt">&lt;h2&gt;</span>Comment Title<span class="nt">&lt;/h2&gt;</span>
  <span class="nt">&lt;p&gt;</span>Content of the comment<span class="nt">&lt;/p&gt;</span>
  <span class="nt">&lt;a&gt;</span>View more<span class="nt">&lt;/a&gt;</span>
<span class="nt">&lt;/div&gt;</span>

<span class="nt">&lt;p</span> <span class="na">id=</span><span class="s">"no-comments"</span> <span class="na">mockle:scenario=</span><span class="s">"comments: None"</span><span class="nt">&gt;</span>
  There aren&#39;t any comments for this post.
<span class="nt">&lt;/p&gt;</span></div>

<p>Let me introduce you to <em>Mockle</em> (which only exists as a concept at the moment; there’s no code available). The main idea is to exploit the fact that every browser supports JavaScript. The template engine you invoke from your application will remove the <code>&lt;script&gt;</code>-tags, so Mockle.js won’t be loaded in production. However, in development (when you simply open the file in the browser) Mockle.js will be loaded and it has the privilege of being able to inspect the HTML and do whatever it likes.</p>

<p>For instance, if you open up the post.html above, it would figure out that it should use layout.html as the layout. Then it could simply fetch the layout using Ajax and wrap it around the template. Boom, dead simple layouts and partials.</p>

<p>To fix the other issue I mentioned above, you could introduce what I call <em>scenarios</em>. By annotating elements as scenarios, Mockle.js could give you a simple UI (floating panel perhaps?) which allows you to enable/disable each choice of the scenario.</p>

<p>I’m also using a quite cool trick: When you supply both a URL and some content to a <code>&lt;script&gt;</code>-tag, the content will simply be ignored. However, you can still easily retrieve the content (both from JavaScript and from any HTML parser). This gives you a convenient place to store metadata for the template.</p>

<p>Maybe the scenario idea isn’t the best solution. Maybe you want to annotate it more like RuHL and less like Effigy. Mockle provides no clear solutions, just a simple idea: <strong>Take advantage of JavaScript to achieve proper Mockup-Driven Development.</strong></p>

<p>Maybe I’ll start hacking on a specific implementation of Mockle. Maybe not. What about you? I’d love to <a href="/comments">hear</a> how this could solve <em>your</em> specific problem.</p>
]]></content>
  </entry>
  <entry>
    <title>Simplicity is difficult</title>
    <id>tag:timeless.judofyr.net,2010-12-26:1293385741</id>
    <link href="http://timelessrepo.com/simplicity-is-difficult"/>
    <updated>2010-12-26T17:49:01Z</updated>
    <published>2010-12-26T17:49:01Z</published>
    <content type="html"><![CDATA[
<p>Advice is really easy to give to others, but really hard to take yourself.</p>

<p>A long time ago, I decided that it was a good idea to fight for simplicity. It’s something to strive for. There’s beauty in simplicity. Yet… simplicity is incredibly difficult. I spend a lot of time on Hacker News, and there’s been a lot of discussion about “Minimum Viable Products.” I don’t find it hard at all to point at someone else’s project and say “You wouldn’t need this, you wouldn’t need that, you could have left this off.” But when looking at my own projects, it’s incredibly hard.</p>

<p>Such was the story with the Hackety Hack 1.0 release. I could have had 1.0 out in September rather than December if I’d only realized just how much stuff I was trying to work on that was absolutely unnecessary. I’m not sure exactly at what point this became clear to me, but I think it was the second or third time someone joked, “You’ve said 1.0 is a week away for the last three months.” It finally hit me: many things weren’t really necessary in a 1.0 release. Many of the things that were taking me so much time were also the parts that had the most bugs, were causing the most problems, and were the most complicated portions.</p>

<p>An interesting aside: I think that git has really enabled me to <a href="http://cam.ly/blog/2010/12/code-fearlessly/">code fearlessly</a>. I don’t worry any more about throwing away code, because it’s all in the history. I don’t mind trying out a new way of doing things, or starting a major refactoring, or anything else: The worst thing that’ll happen is that I’ll be forced to <code>git reset --hard</code> or <code>git branch -D</code>. Oh well. My app will survive.</p>

<p>So that’s what I did for 1.0. I gave the whole application a hard once-over:</p>

<ul>
<li>Does this feature work as intended?</li>

<li>How hard will that bug be to fix?</li>

<li>Is it really necessary?</li>
</ul>

<p>If I didn’t hear the correct answer, I <code>git rm</code>-ed with near reckless abandon. All of that code is going to be back someday, as all of those features are good things. But they weren’t actually needed. They were icing on the cake. What I really needed was to get some steak out the door, and to hell with the sizzle.</p>

<p>I also totally re-wrote the website component for Hackety as well. Once again, I ruthlessly cut scope. I actually cut it farther than I even thought would be okay, but you know what: it was okay. For the first few hours that the site was up, you could ask a question, but there’d be no way to answer it! Of course, later in the evening, I made that possible. But to get a release out the door and in the hands of users, it wasn’t necessary.</p>

<p>Finding what’s absolutely important and what’s not is still an art, at first. Once you have users, you can figure out what they want by tracking what they do, or by asking for feedback directly. But I’ll be constantly repeating to myself, “YAGNI. YAGNI. YAGNI.”</p>
]]></content>
  </entry>
  <entry>
    <title>Charging Objects in Ruby</title>
    <id>tag:timeless.judofyr.net,2010-12-19:1292780941</id>
    <link href="http://timelessrepo.com/charging-objects-in-ruby"/>
    <updated>2010-12-19T17:49:01Z</updated>
    <published>2010-12-19T17:49:01Z</published>
    <content type="html"><![CDATA[
<p>Today we’ll explore a not so widely used feature in Ruby: overriding the the unary operators for fun and profit:</p>

<div><span class="k">class</span> <span class="nc">CustomTimeless</span> <span class="o">&lt;</span> <span class="no">Script</span>
  <span class="c1"># This script applies to every page:</span>
  <span class="o">+</span> <span class="n">url</span><span class="p">(</span><span class="s2">"http://timeless.judofyr.net/*"</span><span class="p">)</span>
  <span class="c1"># ... except the front page:</span>
  <span class="o">-</span> <span class="n">url</span><span class="p">(</span><span class="s2">"http://timeless.judofyr.net/"</span><span class="p">)</span>
  <span class="c1"># ... or the changelog:</span>
  <span class="o">-</span> <span class="n">url</span><span class="p">(</span><span class="s2">"http://timeless.judofyr.net/changelog/*"</span><span class="p">)</span>
  <span class="c1"># ... and only HTML files:</span>
  <span class="o">+</span> <span class="n">type</span><span class="p">(</span><span class="s2">"text/html"</span><span class="p">)</span>
  
  <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>
    <span class="c1"># Do funky thing with the HTML</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>This must be one of the coolest, yet quite unknown, technique in Ruby. For certain types of problems (e.g. when you have a set of rules) this gives you such an elegant way of describing the solution. There’s no meta programming or monkey patching involved, it’s short and sweet and best of all: it’s very intuitive.</p>

<p>Let’s have a look at this and a few other tricks you can do by overriding the unary operators.</p>

<h2 id="you_can_overload_the_unary_operators">You can overload the unary operators???</h2>

<div><span class="n">a</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">b</span> <span class="o">=</span> <span class="o">-</span><span class="n">a</span>
<span class="n">c</span> <span class="o">=</span> <span class="o">+</span><span class="n">a</span>
<span class="n">d</span> <span class="o">=</span> <span class="o">~</span><span class="n">a</span>
<span class="n">e</span> <span class="o">=</span> <span class="o">!</span><span class="n">a</span></div>

<p>You probably already know that it’s possible to override pretty much any operator in Ruby, so why should unary operators be treated differently? It makes very much sense when you think about it, but for some reason, most Rubyists never think about it. Unary operators just seems like a part of the language. Time for some myth busting, eh?</p>

<div><span class="k">class</span> <span class="nc">Rule</span>
  <span class="nb">attr_accessor</span> <span class="ss">:type</span><span class="p">,</span> <span class="ss">:value</span><span class="p">,</span> <span class="ss">:charge</span>
  
  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">type</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
    <span class="vi">@type</span> <span class="o">=</span> <span class="n">type</span>
    <span class="vi">@value</span> <span class="o">=</span> <span class="n">value</span>
    <span class="vi">@charge</span> <span class="o">=</span> <span class="ss">:neutral</span>
  <span class="k">end</span>
  
  <span class="k">def</span> <span class="nf">+@</span>
    <span class="vi">@charge</span> <span class="o">=</span> <span class="ss">:positive</span>
    <span class="nb">self</span>
  <span class="k">end</span>
  
  <span class="k">def</span> <span class="nf">-@</span>
    <span class="vi">@charge</span> <span class="o">=</span> <span class="ss">:negative</span>
    <span class="nb">self</span>
  <span class="k">end</span>
  
  <span class="k">def</span> <span class="nf">~</span><span class="err">@</span>
    <span class="vi">@charge</span> <span class="o">=</span> <span class="ss">:neutral</span>
    <span class="nb">self</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>Woah, woah, woah. Hang on a second. <code>+@</code>? <code>-@</code>? <code>~@</code>? What’s the matter with these weird method names? Let’s fire up IRB and see:</p>

<div><span class="o">&gt;&gt;</span> <span class="n">r</span> <span class="o">=</span> <span class="no">Rule</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">:url</span><span class="p">,</span> <span class="s2">"http://timeless.judofyr.net/*"</span><span class="p">)</span>
<span class="o">=&gt;</span> <span class="c1">#&lt;Rule:0x1003599f8 @value="http://timeless.judofyr.net/*", </span>
     <span class="vi">@charge</span><span class="o">=</span><span class="ss">:neutral</span><span class="p">,</span> <span class="vi">@type</span><span class="o">=</span><span class="ss">:url</span><span class="o">&gt;</span>

<span class="o">&gt;&gt;</span> <span class="n">r</span><span class="p">.</span><span class="nf">charge</span>
<span class="o">=&gt;</span> <span class="ss">:neutral</span>

<span class="o">&gt;&gt;</span> <span class="n">r</span><span class="p">.</span><span class="nf">-</span><span class="err">@</span><span class="p">()</span>
<span class="o">&gt;&gt;</span> <span class="n">r</span><span class="p">.</span><span class="nf">charge</span>
<span class="o">=&gt;</span> <span class="ss">:negative</span>

<span class="o">&gt;&gt;</span> <span class="o">+</span><span class="n">r</span>
<span class="o">&gt;&gt;</span> <span class="n">r</span><span class="p">.</span><span class="nf">charge</span>
<span class="o">=&gt;</span> <span class="ss">:positive</span>

<span class="o">&gt;&gt;</span> <span class="s2">"+(binary)"</span><span class="p">.</span><span class="nf">to_sym</span>
<span class="o">=&gt;</span> <span class="p">:</span><span class="o">+</span>

<span class="o">&gt;&gt;</span> <span class="s2">"+(unary)"</span><span class="p">.</span><span class="nf">to_sym</span>
<span class="o">=&gt;</span> <span class="ss">:+@</span></div>

<p>Like any other method, you can call them the usual way (<code>r.-@()</code>), but it <em>also</em> turns out that Ruby uses these methods internally for the unary operators:</p>

<div><span class="o">&gt;&gt;</span> <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
<span class="o">&gt;&gt;</span> <span class="o">-</span><span class="n">a</span>
<span class="o">=&gt;</span> <span class="o">-</span><span class="mi">1</span>
<span class="o">&gt;&gt;</span> <span class="n">a</span><span class="p">.</span><span class="nf">-</span><span class="err">@</span><span class="p">()</span>
<span class="o">=&gt;</span> <span class="o">-</span><span class="mi">1</span></div>

<p>So how can we (ab)use these methods?</p>

<h2 id="example_proxy_with_html_rewriting_support">Example: Proxy with HTML rewriting support</h2>

<p>Once upon a time there was a <a href="https://github.com/evaryont/mousehole">Ruby proxy</a> which worked pretty much like Greasemonkey: You could easily modify any page <em>before</em> it hit the browser. By using the unary operators you could create scripts and define which pages it should rewrite:</p>

<div><span class="k">class</span> <span class="nc">CustomTimeless</span> <span class="o">&lt;</span> <span class="no">MouseHole</span><span class="o">::</span><span class="no">Script</span>
  <span class="c1"># This script applies to every page:</span>
  <span class="o">+</span> <span class="n">url</span><span class="p">(</span><span class="s2">"http://timeless.judofyr.net/*"</span><span class="p">)</span>
  <span class="c1"># ... except the front page:</span>
  <span class="o">-</span> <span class="n">url</span><span class="p">(</span><span class="s2">"http://timeless.judofyr.net/"</span><span class="p">)</span>
  <span class="c1"># ... or the changelog:</span>
  <span class="o">-</span> <span class="n">url</span><span class="p">(</span><span class="s2">"http://timeless.judofyr.net/changelog/*"</span><span class="p">)</span>
  <span class="c1"># ... and only HTML files:</span>
  
  <span class="k">def</span> <span class="nf">process</span><span class="p">(</span><span class="n">html</span><span class="p">)</span>
    <span class="c1"># Do funky thing with the HTML</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>How does it work? Simply take the Rule class above and combine it with this:</p>

<div><span class="k">class</span> <span class="nc">MouseHole</span><span class="o">::</span><span class="no">Script</span>
  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">rules</span>
    <span class="vi">@rules</span> <span class="o">||=</span> <span class="p">[]</span>
  <span class="k">end</span>
  
  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">url</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="n">rule</span> <span class="o">=</span> <span class="no">Rule</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">:url</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
    <span class="n">rules</span> <span class="o">&lt;&lt;</span> <span class="n">rule</span>
    <span class="n">rule</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>Now you just need to loop through the rules to figure out if an URL matches or not. Ta-da!</p>

<h2 id="terrible_example_negaposi">Terrible Example: Negaposi</h2>

<div><span class="k">class</span>                                                  <span class="nc">NP</span>
<span class="k">def</span>  <span class="nf">initialize</span> <span class="n">a</span><span class="o">=</span><span class="vi">@p</span><span class="o">=</span><span class="p">[],</span> <span class="n">b</span><span class="o">=</span><span class="vi">@b</span><span class="o">=</span><span class="p">[];</span>                      <span class="k">end</span>
<span class="k">def</span> <span class="nf">+@</span><span class="p">;</span><span class="vi">@b</span><span class="o">&lt;&lt;</span><span class="mi">1</span><span class="p">;</span><span class="n">b2c</span> <span class="k">end</span><span class="p">;</span><span class="k">def</span><span class="nf">-@</span><span class="p">;</span><span class="vi">@b</span><span class="o">&lt;&lt;</span><span class="mi">0</span><span class="p">;</span><span class="n">b2c</span>                   <span class="k">end</span>
<span class="k">def</span>  <span class="nf">b2c</span><span class="p">;</span><span class="k">if</span> <span class="vi">@b</span><span class="p">.</span><span class="nf">size</span><span class="o">==</span><span class="mi">8</span><span class="p">;</span><span class="n">c</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="vi">@b</span><span class="p">.</span><span class="nf">each</span><span class="p">{</span><span class="o">|</span><span class="n">b</span><span class="o">|</span><span class="n">c</span><span class="o">&lt;&lt;=</span><span class="mi">1</span><span class="p">;</span><span class="n">c</span><span class="o">|=</span><span class="n">b</span><span class="p">};</span><span class="nb">send</span><span class="p">(</span>
     <span class="s1">&#39;lave&#39;</span><span class="p">.</span><span class="nf">reverse</span><span class="p">,(</span><span class="vi">@p</span><span class="p">.</span><span class="nf">join</span><span class="p">))</span><span class="k">if</span> <span class="n">c</span><span class="o">==</span><span class="mi">0</span><span class="p">;</span><span class="vi">@p</span><span class="o">&lt;&lt;</span><span class="no">c</span><span class="p">.</span><span class="nf">chr</span><span class="p">;</span><span class="vi">@b</span><span class="o">=</span><span class="p">[]</span>  <span class="k">end</span><span class="sh">
     self end end ; begin _ = NP.new                   end

+-+--++----+--+--+---+-------+--++--+++---+-+++-+-+-+++-----+++-_
--++-++--+--+++-++++-++-+++-+-+------+--++++-++---++-++---++-++-_
---------+-+-----+---+--+----+----+--++-                        _</span></div>

<p>Who needs Ruby syntax when you have plus and minus? <a href="http://www.namikilab.tuat.ac.jp/~sasada/diary/200505.html#d10">Negaposi</a> gives you everything you need!</p>

<h2 id="it_gets_even_better">It get’s even better!</h2>

<p>In Ruby 1.9, it gets even better: We can redefine the <code>not</code> operator too:</p>

<div><span class="k">class</span> <span class="nc">Rule</span>
  <span class="nb">attr_accessor</span> <span class="ss">:priority</span>
  
  <span class="k">def</span> <span class="nf">initialize</span>
    <span class="vi">@priority</span> <span class="o">=</span> <span class="mi">0</span>
  <span class="k">end</span>
  
  <span class="c1"># (Only possible in 1.9)</span>
  <span class="k">def</span> <span class="o">!</span>
    <span class="vi">@priority</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="nb">self</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="k">class</span> <span class="nc">Something</span> <span class="o">&lt;</span> <span class="no">Script</span>
  <span class="c1"># !!! VERY IMPORTANT !!!</span>
  <span class="o">!!!</span> <span class="o">+</span> <span class="n">url</span><span class="p">(</span><span class="s2">"http://timeless.judofyr.net/*"</span><span class="p">)</span>
<span class="k">end</span></div>

<h2 id="horrible_example_maybenot_19_only">Horrible Example: MaybeNot (1.9 only)</h2>

<div><span class="k">class</span> <span class="nc">Object</span>
  <span class="k">def</span> <span class="o">!</span><span class="p">;</span> <span class="nb">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">).</span><span class="nf">zero?</span> <span class="k">end</span>
<span class="k">end</span> <span class="k">unless</span> <span class="no">ENV</span><span class="p">[</span><span class="s2">"USER"</span><span class="p">]</span> <span class="o">==</span> <span class="s2">"magnus"</span></div>

<p>“Sorry, mate. I can’t reproduce the error on my machine.”</p>

<h2 id="your_turn">Your turn</h2>

<p>I’d love to see what you can (ab)use this for. Please <a href="/comments">let me know</a> if you find a suitable usage, and I’ll update this article.</p>
]]></content>
  </entry>
  <entry>
    <title>BDD with Rspec and Steak</title>
    <id>tag:timeless.judofyr.net,2010-12-12:1292169710</id>
    <link href="http://timelessrepo.com/bdd-with-rspec-and-steak"/>
    <updated>2010-12-12T16:01:50Z</updated>
    <published>2010-12-12T16:01:50Z</published>
    <content type="html"><![CDATA[
<p>Behavior Driven Development is a test-first software development methodology. It’s a fairly straightforward process if you’re familiar with other agile development methodologies. Here’s a general outline of how software is developed with BDD:</p>

<ul>
<li>All of the stakeholders in the project come together and decide what features the project needs. These “stories” are usually written up on index cards and then organized by priority.</li>

<li>Each story is broken up into smaller “features” that represent one aspect of the story.</li>

<li>An acceptance test is written for the feature.</li>

<li>“The Outer Loop”: Run the acceptance test, watch it fail.</li>

<li>Fix any sort of non-logic error that the failing test gives you.</li>

<li>Re-run the test. Repeat until you get a logical error. This transitions you to the “Inner Loop”</li>

<li>Write a unit test that expresses the desired logic.</li>

<li>Run the test. Watch it fail.</li>

<li>Write the simplest code that passes the unit test.</li>

<li>Run the test, see that it passes.</li>

<li>Refactor code as appropriate.</li>

<li>Pop back into the outer loop: Run the acceptance test again. If your acceptance test passes, congrats! Otherwise, drop back into the inner loop to fix the new logical error.</li>

<li>Refactor one last time, run all of your tests, make sure they all pass.</li>

<li>Congrats! Your feature is ready!</li>
</ul>

<p>This might seem pretty complicated, but it’s actually relatively straightforward. Let’s go through a story that I’m writing for the Hackety Hack website. I’m the only stakeholder, so it’s easy to make executive decisions. Here’s the story:</p>

<p>As a user, I’d like the option to subscribe to forums and threads, and get notifications about updates.</p>

<p>Sounds pretty useful to me! I use email as a to-do list, and I want to make sure I’m kept up-to-date with any discussions. This story can be broken up into a few different features. Here they are:</p>

<pre><code>Scenario: Subscribing to a thread
  Given I&#39;ve commented in a thread
  When someone else makes a comment
  Then I should receive an email
  And it should have a link to that thread

Scenario: Unsubscribing from a thread
  Given I&#39;ve subscribed to a thread
  And I&#39;m on the page for that thread
  When I click the unsubscribe link
  And someone makes a comment
  Then I should not receive an email

Scenario: Subscribing to a forum
  Given I&#39;m on the index page for a forum
  When I click the subscription link
  And someone makes a new thread in that forum
  Then I should receive an email
  And it should have a link to that forum

Scenario: Unsubscribing from a forum
  Given I&#39;ve subscribed to a forum
  And I&#39;m on the index page for that forum
  When I click the unsubscribe link
  And someone makes a new thread in that forum
  Then I should not receive an email</code></pre>

<p>Cool! Four scenarios to make up this feature. You’ll notice that I’m using certain language to talk about each scenario: Given, When, and Then. These words, while not mandatory are helpful: Most features involve some sort of initial system setup, an event that kicks something off, and then an effect that you want to see created. Thinking in these terms can be a useful way of getting started with a story or feature.</p>

<p>Okay! So let’s work on the first scenario: subscribing to a thread. There are a few different libraries that can used with Ruby for acceptance testing. Cucumber can actually be used to read these specifications as I’ve written them, but lately, I’ve come around to a simpler tool: Steak. If you’d like to follow along, you can check out a copy of the site like this:</p>

<pre><code>$ git clone https://github.com/hacketyhack/hackety-hack.com.git
$ rvm install 1.8.7
$ rvm use 1.8.7
$ rvm gemset create hackety-hack.com
$ cd hackety-hack.com
$ git checkout 311d19
$ gem install bundler
$ bundle install</code></pre>

<p>This’ll get your ruby and gemsets set up, check out the revision right before this feature was implemented, and install the gems you need. You might also want to install a copy of MongoDB, you’ll need that, too.</p>

<p>I like to make a separate branch for every feature. This keeps things nice and organized, and if I want to work on multiple features at once, I can make sure they don’t depend on each other in awkward ways. Let’s do that now:</p>

<pre><code>$ git checkout -b subscription_management</code></pre>

<p>Awesome. Time to write an acceptance test! Steak works with RSpec, so the tests are all in the spec/acceptance directory:</p>

<pre><code>$ mvim spec/acceptance/subscription_spec.rb</code></pre>

<p>This’ll open up a blank file. We need to do a little bit of setup, and comment out our English descriptions:</p>

<div><span class="nb">require</span> <span class="no">File</span><span class="p">.</span><span class="nf">dirname</span><span class="p">(</span><span class="kp">__FILE__</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;/acceptance_helper&#39;</span>

<span class="n">feature</span> <span class="s2">"Subscriptions"</span> <span class="k">do</span>

<span class="c1"># Scenario: Subscribing to a thread</span>
<span class="c1">#   Given I&#39;ve commented in a thread</span>
<span class="c1">#   When someone else makes a comment</span>
<span class="c1">#   Then I should receive an email</span>
<span class="c1">#   And it should have a link to that thread</span>

<span class="c1"># Scenario: Unsubscribing from a thread</span>
<span class="c1">#   Given I&#39;ve subscribed to a thread</span>
<span class="c1">#   And I&#39;m on the page for that thread</span>
<span class="c1">#   When I click the unsubscribe link</span>
<span class="c1">#   And someone makes a comment</span>
<span class="c1">#   Then I should not receive an email</span>

<span class="c1"># Scenario: Subscribing to a forum</span>
<span class="c1">#   Given I&#39;m on the index page for a forum</span>
<span class="c1">#   When I click the subscription link</span>
<span class="c1">#   And someone makes a new thread in that forum</span>
<span class="c1">#   Then I should receive an email</span>
<span class="c1">#   And it should have a link to that forum</span>

<span class="c1"># Scenario: Unsubscribing from a forum</span>
<span class="c1">#   Given I&#39;ve subscribed to a forum</span>
<span class="c1">#   And I&#39;m on the index page for that forum</span>
<span class="c1">#   When I click the unsubscribe link</span>
<span class="c1">#   And someone makes a new thread in that forum</span>
<span class="c1">#   Then I should not receive an email</span>

<span class="k">end</span></div>

<p>Okay, now let’s flesh out our first scenario with some Steak:</p>

<div><span class="n">scenario</span> <span class="s2">"Subscribing to a thread"</span> <span class="k">do</span>

  <span class="c1">#Given I&#39;ve commented in a thread</span>
  <span class="n">thread</span> <span class="o">=</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:discussion</span><span class="p">)</span>
  <span class="n">me</span> <span class="o">=</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:hacker</span><span class="p">)</span>
  <span class="n">reply</span> <span class="o">=</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:reply</span><span class="p">,</span> <span class="ss">:author</span> <span class="o">=&gt;</span> <span class="n">me</span><span class="p">.</span><span class="nf">username</span><span class="p">,</span> <span class="ss">:author_email</span> <span class="o">=&gt;</span> <span class="n">me</span><span class="p">.</span><span class="nf">email</span><span class="p">)</span>
  <span class="n">thread</span><span class="p">.</span><span class="nf">replies</span> <span class="o">&lt;&lt;</span> <span class="n">reply</span>

  <span class="no">Pony</span><span class="p">.</span><span class="nf">should_receive</span><span class="p">(</span><span class="ss">:deliver</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">mail</span><span class="o">|</span>
    <span class="n">mail</span><span class="p">.</span><span class="nf">to</span><span class="p">.</span><span class="nf">should</span> <span class="o">==</span> <span class="p">[</span> <span class="n">me</span><span class="p">.</span><span class="nf">email</span> <span class="p">]</span>
    <span class="n">mail</span><span class="p">.</span><span class="nf">body</span><span class="p">.</span><span class="nf">to_s</span><span class="p">.</span><span class="nf">should</span> <span class="o">=~</span> <span class="sr">/\/forums\/</span><span class="si">#{</span><span class="n">thread</span><span class="p">.</span><span class="nf">forum</span><span class="si">}</span><span class="sr">\/</span><span class="si">#{</span><span class="n">thread</span><span class="p">.</span><span class="nf">slug</span><span class="si">}</span><span class="sr">/</span>
  <span class="k">end</span>

  <span class="c1">#When someone else makes a comment</span>
  <span class="n">somebody</span> <span class="o">=</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:hacker</span><span class="p">)</span>
  <span class="n">log_in</span> <span class="n">somebody</span>
  <span class="n">visit</span> <span class="s2">"/forums/</span><span class="si">#{</span><span class="n">thread</span><span class="p">.</span><span class="nf">forum</span><span class="si">}</span><span class="s2">/</span><span class="si">#{</span><span class="n">thread</span><span class="p">.</span><span class="nf">slug</span><span class="si">}</span><span class="s2">"</span>
  <span class="n">click_link</span> <span class="s2">"Reply"</span>
  <span class="n">fill_in</span> <span class="s2">"Body"</span><span class="p">,</span> <span class="ss">:with</span> <span class="o">=&gt;</span> <span class="s2">"Here&#39;s my take on things: Dream big!"</span>
  <span class="n">click_button</span> <span class="s2">"Create Reply"</span>

  <span class="c1">#   Then I should receive an email</span>
  <span class="c1">#   (see pony block above)</span>
  <span class="c1">#   And it should have a link to that thread</span>
  <span class="c1">#   (see pony block above)</span>
<span class="k">end</span></div>

<p>Okay. So. There’s one thing that’s a little weird about this test: we have to say that we expect Pony to receive an email before we create comment #2. This happens in tests that are similar to this one; we’re really testing a side-effect.</p>

<p>Other notable parts of this test: We’re using Factory Girl to set up the state of the world, and Capybara to interact with our application to make the new comment. We’re also using a log_in helper that I’ve written for some of my other tests.</p>

<p>Okay, so we’ve got a test. Let’s watch it fail:</p>

<pre><code>$ rake spec:acceptance
*snip*
Failures:
  1) Subscriptions Subscribing to a thread
     Failure/Error: somebody = Factory(:hacker)
     Validation failed: Username has already been taken</code></pre>

<p>Whoops! Looks like our factory wasn’t set up to generate a series of usernames. The factories are in spec/factories.rb, and here’s the changes we need to make:</p>

<div><span class="no">Factory</span><span class="p">.</span><span class="nf">sequence</span> <span class="ss">:username</span> <span class="k">do</span> <span class="o">|</span><span class="n">n</span><span class="o">|</span>
  <span class="s2">"user</span><span class="si">#{</span><span class="n">n</span><span class="si">}</span><span class="s2">"</span>
<span class="k">end</span>

<span class="c1">#this factory defines a hacker</span>
<span class="no">Factory</span><span class="p">.</span><span class="nf">define</span> <span class="ss">:hacker</span> <span class="k">do</span> <span class="o">|</span><span class="n">u</span><span class="o">|</span>
  <span class="n">u</span><span class="p">.</span><span class="nf">username</span> <span class="p">{</span> <span class="no">Factory</span><span class="p">.</span><span class="nf">next</span><span class="p">(</span><span class="ss">:username</span><span class="p">)}</span></div>

<p>Okay, let’s run those tests again:</p>

<pre><code>$ rake spec:acceptance
*snip*
Failures:
  1) Programs can be created
     Failure/Error: page.should have_content &quot;By steve&quot;
     expected #has_content?(&quot;By steve&quot;) to return true, got false
     # ./spec/acceptance/programs_spec.rb:18

  2) Subscriptions Subscribing to a thread
     Failure/Error: Pony.should_receive(:deliver) do |mail|
     (Pony).deliver(any args)
         expected: 1 time
         received: 0 times
     # ./spec/acceptance/subscription_spec.rb:13</code></pre>

<p>Whoops! This is both good and bad. Test failure #2 is our logical error: the test ran, but Pony was expecting an email, and it didn’t get one. The first error is something that happens sometimes: we have a more brittle test than we thought. Rather than write a test that actually looks up a username, I hardcoded a username. Whoops! After fixing that, we just get the expectation failure. Time to drop into the inner loop.</p>

<p>Our unit tests should be testing a few things: making a Reply should create a subscription to a thread, and it should also send a mail to everyone who has a subscription. Unit tests are kept in the spec directory. Let’s check out our Reply tests:</p>

<pre><code>$ mvim spec/reply_spec.rb</code></pre>

<p>… and there aren’t any. So let’s set that up:</p>

<div><span class="nb">require</span> <span class="no">File</span><span class="p">.</span><span class="nf">expand_path</span><span class="p">(</span><span class="kp">__FILE__</span> <span class="o">+</span> <span class="s2">"/../spec_helper"</span><span class="p">)</span>

<span class="n">describe</span> <span class="no">Reply</span> <span class="k">do</span>

  <span class="n">it</span> <span class="s2">"should do something"</span>

<span class="k">end</span></div>

<p>And run it:</p>

<pre><code>$ rake spec
*snip*
Pending:
  Reply should do something
    # Not Yet Implemented</code></pre>

<p>I currently have ‘rake spec’ set up to run all of my specs, so we also get the failing acceptance test as well as the pending unit test. As your test suite grows, you’ll generally break these out in a more fine-grained way, but the tests only take about two seconds to run, so I haven’t found the need to yet.</p>

<p>Okay, let’s actually write our unit tests:</p>

<div><span class="n">describe</span> <span class="s2">"subscriptions"</span> <span class="k">do</span>
  <span class="n">it</span> <span class="s2">"adds one to its Discussion upon creation"</span> <span class="k">do</span>
    <span class="n">discussion</span> <span class="o">=</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:discussion</span><span class="p">)</span>
    <span class="n">discussion</span><span class="p">.</span><span class="nf">subscribed_users</span><span class="p">.</span><span class="nf">count</span><span class="p">.</span><span class="nf">should</span> <span class="o">==</span> <span class="mi">0</span>
    <span class="n">discussion</span><span class="p">.</span><span class="nf">replies</span> <span class="o">&lt;&lt;</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:reply</span><span class="p">)</span>
    <span class="n">discussion</span><span class="p">.</span><span class="nf">subscribed_users</span><span class="p">.</span><span class="nf">count</span><span class="p">.</span><span class="nf">should</span> <span class="o">==</span> <span class="mi">1</span>
  <span class="k">end</span>

  <span class="n">it</span> <span class="s2">"triggers an email to others upon creation"</span>
<span class="k">end</span></div>

<p>One written, one pending. And run them:</p>

<pre><code>$ rake spec
*snip*
2) Reply subscriptions adds one to its Discussion upon creation
   Failure/Error: discussion.subscribed_users.count.should == 0
   undefined method `subscribed_users&#39; for #&lt;Discussion:0x10355b1e0&gt;</code></pre>

<p>Let’s create that method, and re run the spec:</p>

<pre><code>$ rake spec
*snip*
2) Reply subscriptions adds one to its Discussion upon creation
   Failure/Error: discussion.subscribed_users.count.should == 0
   undefined method `count&#39; for nil:NilClass</code></pre>

<p>Okay, we’ve set up our method correctly, but it’s returning nil rather than some kind of Enumerable. Simplest thing? An empty array. Rerun spec time!</p>

<pre><code>$ rake spec
*snip*
2) Reply subscriptions adds one to its Discussion upon creation
   Failure/Error: discussion.subscribed_users.count.should == 1
   expected: 1,
        got: 0 (using ==)</code></pre>

<p>Now that I’m looking at this test, though, it seems a little bit bad. Why am I asking for all of this stuff about a discussion? This is a reply test? And besides, if I write the after_create handler for the reply right now, it’s going to have to mess around with the internals of its Discussion to do its job. That’s a poor separation of concerns. Let’s back up:</p>

<pre><code>$ git checkout models/discussion.rb</code></pre>

<p>And rework our test:</p>

<div><span class="n">it</span> <span class="s2">"adds one to its Discussion upon creation"</span> <span class="k">do</span>
  <span class="n">email</span> <span class="o">=</span> <span class="s2">"someone@example.com"</span>
  <span class="vi">@discussion</span> <span class="o">=</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:discussion</span><span class="p">)</span>
  <span class="vi">@discussion</span><span class="p">.</span><span class="nf">should_receive</span><span class="p">(</span><span class="ss">:create_subscription!</span><span class="p">)</span>
  <span class="vi">@discussion</span><span class="p">.</span><span class="nf">replies</span> <span class="o">&lt;&lt;</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:reply</span><span class="p">,</span> <span class="ss">:author_email</span> <span class="o">=&gt;</span> <span class="n">email</span><span class="p">)</span>
  <span class="vi">@discussion</span><span class="p">.</span><span class="nf">save</span>
<span class="k">end</span></div>

<p>This is much more clear. We tell the discussion that someone would like to subscribe to it. And running our test tells us what is needed next:</p>

<pre><code>2) Reply subscriptions adds one to its Discussion upon creation
   Failure/Error: discussion.should_receive(:create_subscription!).with(email)
   (Double Discussion).create_subscription!(&quot;someone@example.com&quot;)
       expected: 1 time
       received: 0 times</code></pre>

<p>So let’s do that in models/reply.rb:</p>

<div><span class="n">after_save</span> <span class="ss">:send_subscription_notice</span>

<span class="kp">private</span>

<span class="k">def</span> <span class="nf">send_subscription_notice</span>
  <span class="n">_root_document</span><span class="p">.</span><span class="nf">create_subscription!</span> <span class="ss">:author_email</span>
<span class="k">end</span></div>

<p>Two things about this: First of all, I’d really like this to be an after_create, not an after_save. It appears as though MongoMapper doesn’t run after_create with embedded documents, though. I’m going to have to look into this a bit further before committing this code. after_save is good enough for now. Secondly, to reach back up out of the embedded document, we need to use the <em>root</em>document accessor. This starts with an _, and so I’m not sure that it’s the best way to accomplish this. _s usually indicate private things that you shouldn’t mess with. So I have two things that are kinda bad, but they’re good enough for now. That’s the whole point of being Timeless, eh?</p>

<p>Anyway, now we’re getting three failures:</p>

<pre><code>3) Subscriptions Subscribing to a thread
   Failure/Error: click_button &quot;Create Reply&quot;
   undefined method `create_subscription!&#39; for #&lt;Discussion:0x1038a3348&gt;
   # ./models/reply.rb:22:in `send_subscription_notice&#39;</code></pre>

<p>So it’s time to add this method to Discussion. Let’s pop open the (nonexistant) Discussion tests</p>

<pre><code>$ mvim spec/discussion_spec.rb</code></pre>

<p>and add a test:</p>

<div><span class="n">describe</span> <span class="no">Discussion</span> <span class="k">do</span>
  
  <span class="n">describe</span> <span class="s2">"#create_subscription!"</span> <span class="k">do</span>

    <span class="n">it</span> <span class="s2">"adds a new email to the subscription list"</span> <span class="k">do</span>
      <span class="n">discussion</span> <span class="o">=</span> <span class="no">Factory</span><span class="p">(</span><span class="ss">:discussion</span><span class="p">)</span>
      <span class="n">discussion</span><span class="p">.</span><span class="nf">subscribed_users</span><span class="p">.</span><span class="nf">count</span><span class="p">.</span><span class="nf">should</span> <span class="o">==</span> <span class="mi">0</span>
      <span class="n">discussion</span><span class="p">.</span><span class="nf">create_subscription!</span> <span class="s2">"somebody@example.com"</span>
      <span class="n">discussion</span><span class="p">.</span><span class="nf">subscribed_users</span><span class="p">.</span><span class="nf">count</span><span class="p">.</span><span class="nf">should</span> <span class="o">==</span> <span class="mi">1</span>
    <span class="k">end</span>

  <span class="k">end</span>

<span class="k">end</span></div>

<p>And run them:</p>

<pre><code>$ rake spec
*snip*
4) Discussion#create_subscription! adds a new email to the subscription list
   Failure/Error: discussion.subscribed_users.count.should == 0
   undefined method `subscribed_users&#39; for #&lt;Discussion:0x1037ec3a0&gt;</code></pre>

<p>Okay, so we have no subscribed_users. Let’s add an array of Strings to our model, to store the emails of subscribers:</p>

<p lang="ruby">key :subscribed_users, Array</p>

<p>And run some specs:</p>

<pre><code>$ rake spec
4) Discussion#create_subscription! adds a new email to the subscription list
   Failure/Error: discussion.create_subscription! &quot;somebody@example.com&quot;
   undefined method `create_subscription!&#39; for #&lt;Discussion:0x10384c570&gt;</code></pre>

<p>Okay! Time to get rid of most of these errors. Let’s add an create_subscription! method:</p>

<div><span class="k">def</span> <span class="nf">create_subscription!</span>
<span class="k">end</span></div>

<p>And run some specs (tired of this yet?)</p>

<pre><code>$ rake spec
4) Discussion#create_subscription! adds a new email to the subscription list
   Failure/Error: discussion.create_subscription! &quot;somebody@example.com&quot;
   wrong number of arguments (1 for 0)
   # ./spec/discussion_spec.rb:8:in `create_subscription!&#39;</code></pre>

<p>Whoops! Let’s add that argument:</p>

<div><span class="k">def</span> <span class="nf">create_subscription!</span> <span class="n">email</span>
<span class="k">end</span></div>

<p>And run our specs:</p>

<pre><code>$ rake spec
Failures:
  1) Subscriptions Subscribing to a thread
     Failure/Error: Pony.should_receive(:deliver) do |mail|
     (Pony).deliver(any args)
         expected: 1 time
         received: 0 times
     # ./spec/acceptance/subscription_spec.rb:13

  2) Discussion#create_subscription! adds a new email to the subscription list
     Failure/Error: discussion.subscribed_users.count.should == 1
     expected: 1,
          got: 0 (using ==)
     # ./spec/discussion_spec.rb:9</code></pre>

<p>Two big failures here: our acceptance test isn’t seeing an email, and our subscription isn’t adding anything to our array. Let’s finish off our unit test before bouncing back into acceptance-land:</p>

<div><span class="k">def</span> <span class="nf">create_subscription!</span> <span class="n">email</span>
  <span class="n">subscribed_users</span> <span class="o">&lt;&lt;</span> <span class="n">email</span>
<span class="k">end</span></div>

<p>And run our specs:</p>

<pre><code>$ rake spec
Pending:
  Reply subscriptions triggers an email to others upon creation
    # Not Yet Implemented
    # ./spec/reply_spec.rb:12

Failures:
  1) Subscriptions Subscribing to a thread
     Failure/Error: Pony.should_receive(:deliver) do |mail|
     (Pony).deliver(any args)
         expected: 1 time
         received: 0 times
     # ./spec/acceptance/subscription_spec.rb:13

Finished in 1.44 seconds
33 examples, 1 failure, 1 pending</code></pre>

<p>And there it is: our second unit test that’s still pending is testing our email, and the integration test isn’t seeing it, either. But we’ve successfully navigated adding a subscription!</p>

<p>I’m going to continue updating this article in the future, going through the rest of the process of adding the rest of the necessary specs and features. As always, <a href="/comments">comments are always welcomed</a>.</p>

<p>Thanks to Dan Dorman and Lonny Eachus for a few suggestions and fixes.</p>
]]></content>
  </entry>
  <entry>
    <title>Meta: Introducing Steve</title>
    <id>tag:timeless.judofyr.net,2010-12-12:1292164001</id>
    <link href="http://timelessrepo.com/changelog/8"/>
    <updated>2010-12-12T14:26:41Z</updated>
    <published>2010-12-12T14:26:41Z</published>
    <content type="html"><![CDATA[
<p>I’d like you to welcome our newest author here at Timeless: <strong>Steve Klabnik</strong>. You may already know him as the maintainer of <a href="http://hackety-hack.com/">Hackety Hack</a>, a contributor to <a href="http://shoesrb.com/">Shoes</a> or just a pleasant human being, and I’m confident that he’ll continue his awesomeness over here at Timeless.</p>

<p>The stage is yours, Steve.</p>
]]></content>
  </entry>
  <entry>
    <title>Why there is no talent</title>
    <id>tag:timeless.judofyr.net,2010-12-08:1291833930</id>
    <link href="http://timelessrepo.com/why-there-is-no-talent"/>
    <updated>2010-12-08T18:45:30Z</updated>
    <published>2010-12-08T18:45:30Z</published>
    <content type="html"><![CDATA[
<p><em>Note: This is a more scientific follow-up to another essay called <a href="/there-is-no-talent">“There is no talent”</a>. I recommend you to read that essay first, make up your mind about it and then continue with this article. At any time, feel free to <a href="/comments">contact me</a> if you have any questions or something to add.</em></p>

<p>A few days ago I published an essay called <a href="/there-is-no-talent">“There is no talent”</a>, which is by far the most popular essay I’ve written here. The general response seems to be that most people agree somewhat with the essay, but there’s also a significant group who agree completely and a smaller group who disagree completely. To be honest, I’m not surprised that I’ve received so many messages from people who partially or fully disagree with the previous post because the essay wasn’t really making a scientific argument, but rather an emotional one.</p>

<p>The goal of the “There is no talent” essay was in fact not to convince people that there really is no such thing as “talent”. No, no, no. The point was to inspire people to forget about prejudices and simply start following their passion. I’ve received several comments which seem to confirm that not thinking about talent in general have a positive effect on their life:</p>

<p>From Martin Berntsen:</p>

<blockquote>
<p>The things you point out at the end is just so very true. My experiences are similar to the ones you write about. I didn’t do anything special at a young age, my Nintendo 64 and PlayStation 2 was more than interesting enough, but then a couple of years ago, my interest for music and programming was growing. In the beginning I wasn’t very good at it of course, and some of my friends were like: “Why even bother?” But I just kept going and I got better, I’m still learning, and it feels great. I think many people around our age give up on their passions in fear of what others might think, which is very sad indeed.</p>
</blockquote>

<p>From Blake Johnson:</p>

<blockquote>
<p>It’s only in the past 5 years I’ve really come to this realization, and it’s made a huge impact in how I live my life. Once you realize that there’s no such thing as talent, you can let go of the idea that you can’t do this or you can’t do that because you are not “good” at them. You can just start getting better at them and developing “talent”.</p>
</blockquote>

<p>While trying to make this simple point, I made several bold statements which were far from scientifically (or even logically) explained. The fact that some of these might have been wrong doesn’t really concern me. I wrote the essay to provoke thoughts, not to tell you what you already knew. If you’ve found flaws in my logic, that means I’ve done my job.</p>

<p>However, I find the more “scientific” discussion around this topic very interesting too, and in this essay I will try to sum up the comments and thoughts I’ve received so far. I’ve essentially based this on emails sent directly to me and <a href="http://news.ycombinator.com/item?id=1971641">the comments posted on Hacker News</a>.</p>

<h2 id="definitions_definitions_definitions">Definitions, definitions, definitions</h2>

<p>We can’t discuss anything if we don’t agree what the words mean in the first place.</p>

<p>Let’s start with <em>talent</em>. Talent is usually defined as “a marked innate ability”. The most important words here are <em>innate</em> and <em>ability</em>. Innate means that it’s something you’ve born with: something which, straight from day 1 of your life, is always within your body (even though it might take some time to materialize). The fact that it’s an ability rules out any physical attributes (such as weight, height etc) since these are merely facts. However, these facts can lead to an ability. By being tall you may have a natural talent for blocking basketball shots. Effectively, this means that we’ll have to include these as a part of the concept talent.</p>

<p><em>Passion</em> is a little trickier though. The definition which matches what I mean by passion in these essays must be “boundless enthusiasm” or “a strong affection or enthusiasm for an object, concept, etc.” Passion is when you continue doing something, not because you have a specific goal in mind (better grades, impress friends), but because you simply enjoy it.</p>

<p>Now that we have the proper definitions in place, we can try to answer the original superficial hypothesis presented:</p>

<h2 id="is_there_really_such_a_thing_called_talent">Is there really such a thing called <em>talent</em>?</h2>

<p>Short answer: Yes.</p>

<p>Long answer: Trying to prove that “marked innate abilities” don’t exist is indeed a tricky process. For instance, we can clearly see that most people have an unique appearance, which implies that at least some parts of our body are “randomly generated”. What makes the brain so special that it should be universally equal among all humans? Because if it’s it not equal, that means it’s different and that again leads to the conclusion that some are better suited to perform specific tasks. Well, because we also include physical advantages in our concept of talent, we can simply draw the conclusion based on the fact that some people are taller than other.</p>

<p>The fact that “marked innate abilities” do actually exist isn’t that interesting. Let’s move over to the real questions:</p>

<h2 id="how_much_does_talent_really_matter">How much does talent really matter?</h2>

<p>How much does talent really matter? How much can you accomplish without having a marked innate ability for something? How much can you accomplish if you have a marked innate ability, but don’t spend your time working on it?</p>

<p>I think we all agree on that in order to end up in the world-class of anything, everything needs to be a perfect match: Your innate abilities, working on it from your childhood, family, friends and coaches which always supports you, and so on. For this discussion, I think it’s fair to say that we can ignore these exceptional people, but rather focus on the great people.</p>

<p>There’s various ways to become great at something. One way is based “purely” on your talent. From <a href="http://news.ycombinator.com/item?id=1973664">donaq</a>:</p>

<blockquote>
<p>Back when I was doing my compulsory military service, I was really passionate about basketball. I used to play almost everyday, and there was a team in my neighbourhood with a coach I trained with twice a week. Then I met this guy in my platoon who, though shorter than me, trashed me soundly every time we played. I would have thought someone like that would need to constantly hone his skills, but he didn’t. He just occasionally joined us for a game. For about a year before he completed his service I (and a couple of other guys in the platoon) trained to beat him, but never came close. The interesting thing was this one conversation we had where he confessed, “I don’t like basketball. I only play because I am good.”</p>
</blockquote>

<p>Another way is based “purely” on environment. From <a href="http://www.agameacademy.org/images/TheMakingofAnExpert.pdf"><em>The Making of an Expert</em></a> by K. Anders Ericsson:</p>

<blockquote>
<p>Thirty years ago, two Hungarian educators, László and Klara Polgár, decided to challenge the popular assumption that women don’t succeed in areas requiring spatial thinking, such as chess. They wanted to make a point about the power of education. The Polgárs homeschooled their three daughters, and as part of their education the girls started playing chess with their parents at a very young age. Their systematic training and daily practice paid off. By 2000, all three daughters had been ranked in the top ten female players in the world. The youngest, Judit, had become a grand master at age 15, breaking the previous record for the youngest person to earn that title, held by Bobby Fischer, by a month. Today Judit is one of the world’s top players and has defeated almost all the best male players.</p>
</blockquote>

<p>Let’s follow Ericsson’s thoughts a bit. In fact, Ericsson has actually done some great research on this topic. In the paper <a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.169.9712&rep=rep1&type=pdf"><em>The Role of Deliberate Practice in the Acquisition of Expert Performance</em></a> from 1993, Ericsson presents results from several studies about the role of deliberate practice in the acquisition of expert performance (well, duh). By “expert performance”, Ericsson essentially means what I’ve described as “being great at something”, and “exceptional performance” is pretty close to “exceptional people”.</p>

<p>I highly recommend reading the whole paper, but I’ll try to show you the most important parts.</p>

<blockquote>
<p>There is a relatively widespread conception that if individuals are innately talented, they can easily and rapidly achieve an exceptional level of performance once they have acquired basic skills and knowledge.</p>
</blockquote>

<p>This is sort-of the same conception I was trying to disprove (on an emotional level). Ericsson follows up with more scientific proofs: He refers to another study that came to the conclusion that nobody had attained the level of an international chess master (grandmaster) “with less than about a decade’s intense preparation with the game”. On average, for players that started studying chess after age 11, it took <strong>11.7 years</strong> to reach an international chess master status. For players that started even earlier, it took <strong>16.5 years</strong>. Even famous chess players such as Bobby Fischer and Salo Flohr needed about 10 years, only one year “faster” than the average. This “10-year-rule” has been supported by data from several other fields, including music, mathematics, tennis, swimming, and long-distance running.</p>

<p>This tells us that even though you might have a big talent for something, you’ll still have to invest a huge amount of time in order to become great at it. There’s simply no shortcut for talented people. In the long run, this means that being talented alone won’t help you to reach expertise, even though it certainly might help.</p>

<p>Of course, there are several ways you can spend 10 years, and they don’t all lead to the same place. Ericsson mentions three types of activities:</p>

<blockquote>
<p>Consider three general types of activities, namely, work, play, and deliberate practice. Work includes public performance, competitions, services rendered for pay, and other activities directly motivated by external rewards. Play includes activities that have no explicit goal and that are inherently enjoyable. Deliberate practice includes activities that have been specially designed to improve the current level of performance. The goals, costs, and rewards of these three types of activities differ, as does the frequency with which individuals pursue them.</p>
</blockquote>

<p>He goes on to discuss the differences between these activities. Work activities are far from optimal as ways to learn because there’s another, clearly expected outcome from the activity (to get the job done). Play activities don’t really have a concrete outcome, and therefore they provide more opportunities for learning. Deliberate practice on the other hand has improvements in performance as the primary goal, which obviously makes learning the expected result.</p>

<p>There’s an important difference between just practicing and what Ericsson calls “deliberate practicing”. <a href="http://news.ycombinator.com/item?id=1971781">mechanical_fish</a> touches this with his comment:</p>

<blockquote>
<p>At the risk of overgeneralizing, the most important talent is meta-analysis. You have to be able to self-analyze and self-correct. (If you can’t hear the fact that you’re unable to play on the beat or keep a steady rhythm it doesn’t matter how many guitar chords you know.) You need the social skills and awareness to seek out criticism, listen to it, and act upon it. (If you don’t hear your fellow musicians dropping hints, or don’t act upon those hints, you won’t get better.) And you have to cultivate abilities that may seem unrelated to the problem at hand… because you recognize, consciously or instinctively, that they are essential to your goal. (Professional scientists, for example, need a lot of sales, political, management, and literary skills. And, famously, our stock consumer PCs ship with lots of fonts in part because Steve Jobs audited a calligraphy class during his brief career as a college dropout.)</p>
</blockquote>

<p>Practicing by itself is worthless if you don’t constantly make sure you’re practicing the right things. This requires both meta-analysis and feedback. Once again from Ericsson: “In the absence of adequate feedback, efficient learning is impossible and improvement only minimal even for highly motivated subjects. Hence mere repetition of an activity will not automatically lead to improvement in, especially, accuracy of performance.”</p>

<p>The biggest problem by learning through work is that exploration is generally not encouraged. You’re supposed to give the best performance in the least amount of time, and “hence individuals rely on previously well-entrenched methods rather than exploring alternative methods with unknown reliability. The costs of mistakes or failures to meet deadlines are generally great, which discourages learning and acquisition of new and possibly better methods during the time of work.”</p>

<p>Play activities don’t have the same pressure to perform, so you can more easily explore and expand your abilities. On the other hand, because there’s no pressure at all, it’s also easier to continue playing with what you already know and enjoy. There’s nothing to stop you from taking a break when it becomes too difficult, and unless you have the mentality to turn play into deliberate practice, you’ll never get any better.</p>

<p>Based on these thoughts, I believe it’s fair to draw the conclusion that in many situations, talent has little direct effect on the outcome. And even in fields where physical advantages are required in order to reach an expert performance (like sports), you’ll still has to invest a huge amount of time in it.</p>

<h2 id="what_about_passion">What about passion?</h2>

<p>Well, so what does passion have to do with all of this?</p>

<p>I don’t have any scientific papers to refer to, but please follow along anyway. Imagine that you must invest 10 years of your life in one specific field of study. <em>10 years</em>. Possibly over 10% of your lifetime. What are your reasons? I would claim that, if we ignore external coercion, you simply need to have a passion in order to continue practicing and improving. Passion leads to practicing, and practicing (deliberate at least) leads to skills. I believe that passion is the only way to became great at something without affecting your quality of life in a negative way.</p>

<p>But wait a second, couldn’t you consider passion as a talent? By our current definitions, this is a rather tricky question since it involves yet another “nature vs nurture” debate. How much can the environment affect our feelings? How much is decided from the beginning? I’m not even going to consider discussing this…</p>

<h2 id="conclusion">Conclusion</h2>

<p>In order to become great or excellent, you must keep practicing. Not mindless practice of course, but deliberate practice as Ericsson has described. Depending on the subject, the innate abilities or the physical attributes may help, but is not essential for reaching an expert performance. In many cases, having a talent makes no significant differences on the outcome, which therefore leads to the conclusion that, very often, “there is no talent”.</p>

<p>If talent doesn’t matter, what do you need then? Passion, passion and passion. By following your passion you will not only get better at things, but your life situation will also improve since you’re doing what you really want to do. Wonderful, isn’t it?</p>

<p><em>(Thanks to Peter Aronoff for reading drafts and correcting all my silly mistakes)</em></p>
]]></content>
  </entry>
  <entry>
    <title>There is no talent</title>
    <id>tag:timeless.judofyr.net,2010-12-05:1291558561</id>
    <link href="http://timelessrepo.com/there-is-no-talent"/>
    <updated>2010-12-05T14:16:01Z</updated>
    <published>2010-12-05T14:16:01Z</published>
    <content type="html"><![CDATA[
<p>I’m good with computers. At least compared with others at my age. And believe me, I’ve heard it a lot: “Wow, you really have a talent there!” Well, apparently I <em>do</em> have a talent for computers. Or …?</p>

<p>Let’s try an experiment: Take all of my knowledge of computer related matters and divide it by the number of hours I’ve spent in front of my computer. That’s my “speed” of learning. Then let’s do the same with some of my friends, who are not so “technically inclined”. Whose score would be largest? If I have a talent for computers, it would make sense that I learn it faster than others, right?</p>

<p>Truth is, I believe I would have scored far lower than “regular” people. I’ve spent <em>tons</em> of hours in front of my computer and very often I don’t learn anything new or create something different. Heck, I’ve probably been “wasting” more hours than many non-technically inclined have spent <em>in total</em>. That doesn’t sound like a talent?</p>

<p>I believe that <strong>there is no such thing as talent</strong>.</p>

<p>There is only passion. I have a <em>passion</em> for computers, computer science, programming, creating things, sharing things, helping others and so on. That’s why I’m willing to spend tons of hours on it: Because it’s fun; because I like it; because it’s what I want to do. That’s why I decided to do it.</p>

<p>In fact, I remember my first encounter with programming quite well. My father has always been working with system management, and I never really realized what he <em>actually</em> did on a day-to-day basis. It was all very distant to me. However, one day he had to write a small application for a client, and he ended up doing at least some parts of it at home. I didn’t quite understand what the program was supposed to do, or how Dad managed to make it do so, but I would just sit right next to him and <em>watch</em> him work. Not asking questions or trying to understand anything (he was working after all), but simply watching was interesting enough for me.</p>

<p>And that’s what triggered my passion. Suddenly I started experimenting with FrontPage, HTML, JavaScript (mostly copy-pasting snippets from other places), PHP and MySQL. I was constantly looking for a better free hosting service, and looking all over the internet for PHP scripts I could install and enhance. How I loved HotScripts! Those were some years full of experimenting and fun, without a clear understanding about how the technologies worked (both together and apart).</p>
<hr />
<p>The great thing about starting with this while you’re young is that nobody judges you. You are <em>supposed</em> to just having fun, and even if something is way out of your league they won’t burst your bubble, but rather let you continue experimenting.</p>

<p>As soon as you grow up, people will have expectations about you. “You can’t sing.” “You want to paint? Just give up already!” “Skating? Man, you’re wasting your time!” They might not say it like that, but if you’re doing something beyond people’s expectation, you will be met by skepticism. People say that it’s good to “try something new”, but the moment the “something” is considered difficult, you’re “wasting your time” instead.</p>

<p>You will look around and find other people at your age who are way better than you at playing guitar, skating or singing. How can you possibly be as good as them? They must have a talent for it! But in reality, all they did was starting earlier than you or have been spending more time doing it.</p>

<p>It’s so easy to give up; it might almost seem like the whole world is against you. Well, forget about the rest of the world. There is no such thing as talent, and in the end you <em>will</em> end up as skilled as your “talented” heros.</p>

<p>Simply by following your passion.</p>

<p><em>— Magnus Holm</em></p>
]]></content>
  </entry>
  <entry>
    <title>Refinements in Ruby</title>
    <id>tag:timeless.judofyr.net,2010-11-28:1290957280</id>
    <link href="http://timelessrepo.com/refinements-in-ruby"/>
    <updated>2010-11-28T15:14:40Z</updated>
    <published>2010-11-28T15:14:40Z</published>
    <content type="html"><![CDATA[
<p>At RubyConf 2010 Shugo Maeda <a href="http://www.rubyconf.org/presentations/51">talked about <em>Refinements</em></a>: A proposal for a new feature in Ruby which allows you to easily override methods without affecting other parts of your program:</p>

<div><span class="k">module</span> <span class="nn">TimeExtensions</span>
  <span class="n">refine</span> <span class="no">Fixnum</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nf">minutes</span><span class="p">;</span> <span class="nb">self</span> <span class="o">*</span> <span class="mi">60</span><span class="p">;</span> <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="k">class</span> <span class="nc">MyApp</span>
  <span class="n">using</span> <span class="no">TimeExtensions</span>

  <span class="k">def</span> <span class="nf">initialize</span>
    <span class="nb">p</span> <span class="mi">2</span><span class="p">.</span><span class="nf">minutes</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="no">MyApp</span><span class="p">.</span><span class="nf">new</span>    <span class="c1"># =&gt; 120</span>
<span class="nb">p</span> <span class="mi">2</span><span class="p">.</span><span class="nf">minutes</span>  <span class="c1"># =&gt; NoMethodError</span></div>

<p>Let’s have a look at the <em>why’s</em> and <em>how’s</em> of this proposal.</p>

<h2 id="the_power_of_monkey_patching">The Power of Monkey Patching</h2>

<p>Ruby allows you to both open previously defined classes and redefine any method. In addition, Ruby doesn’t treat core classes any differently from user-defined classes, so this gives you a lot of power to completely change the behaviour Ruby. This is of course a double edged sword: You can more easily change Ruby to match your thoughts (rather than changing your thoughts to match Ruby), but it also means that everyone else now needs to follow your rules.</p>

<p>Getting everyone to play along nicely has proven to be a challenge, and the solution has always been solved socially. As long as two core teams (let’s say Rails and DataMapper) work together, they can quite easily solve any problems, but the real issue is when you, as a user, want to use two libraries together. The libraries may work perfectly separately, but the moment you combine them you’ll get some weird behaviour. There’s not really much to can do, other than waiting for the library to be updated (or do the work yourself).</p>

<h2 id="a_wild_classbox_appears">A wild Classbox appears!</h2>

<p>If you’ve been following the development of Ruby, you may have heard of <a href="http://scg.unibe.ch/research/classboxes"><em>classboxes</em></a>. They were first introduced by Alexandre Bergel, Stéphane Ducasse, and Roel Wuyts in 2003 by the paper <a href="http://scg.unibe.ch/archive/papers/Berg03aClassboxes.pdf">Classboxes: A Minimal Module Model Supporting Local Rebinding</a>. It’s essentially a way to monkey patch classes and methods, but only within the context of <em>your</em> code and not globally. At the moment, it’s been implemented in Smalltalk (Squak), Java and .Net, but there’s also been some work at trying to apply it to Ruby.</p>

<p>The refinements proposal by Shugo captures the same idea as classboxes, but it behaves slightly differently in certain cases (we’ll get back to those in a minute). The differences are not big enough to justify having both refinements and classboxes, so expect this to be the only way to safely monkey patch in Ruby in the following years (if the proposal gets accepted of course).</p>

<p>While this is still only a proposal, Shugo has actually implemented it in Ruby 1.9 <em>and</em> provided a patch, so why not install it right away so you can play with it as we go through the features?</p>

<h2 id="installing">Installing</h2>

<p>In order to install the refinements version of Ruby, you need to grab <strong>r29837</strong> of <a href="http://svn.ruby-lang.org/repos/ruby/trunk">trunk</a> and apply <a href="http://stuff.judofyr.net/refinements.diff">the refinements-patch</a>. If you’re using <a href="http://rvm.beginrescueend.com/">rvm</a>, it’s as simple as:</p>

<pre><code>$ curl -O http://stuff.judofyr.net/refinements.diff
$ rvm install ruby-head-r29837 --patch refinements.diff
$ rvm ruby-head-r29837</code></pre>

<p>Or manually:</p>

<pre><code>$ svn checkout -q -r 29837 http://svn.ruby-lang.org/repos/ruby/trunk ruby-refinements
$ cd ruby-refinements
$ curl http://stuff.judofyr.net/refinements.diff | patch -p1
$ autoconf
$ ./configure --prefix /usr/local/ruby-refinements
$ make
$ make install
$ export PATH=&quot;/usr/local/ruby-refinements/bin:$PATH&quot;</code></pre>

<p>Now you should be able to run all of the examples given in this article.</p>

<h2 id="refine_dont_redefine">Refine, don’t redefine</h2>

<p>Instead of redefining or defining new methods directly on classes, you’ll create <em>refinements</em>:</p>

<div><span class="k">module</span> <span class="nn">JSONGenerator</span>
  <span class="n">refine</span> <span class="no">String</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nf">to_json</span><span class="p">;</span> <span class="nb">inspect</span> <span class="k">end</span>
  <span class="k">end</span>
  
  <span class="n">refine</span> <span class="no">Fixnum</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nf">to_json</span><span class="p">;</span> <span class="nb">to_s</span> <span class="k">end</span>
  <span class="k">end</span>
  
  <span class="n">refine</span> <span class="no">Array</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nf">to_json</span>
      <span class="c1"># Refinements can see one another, so we can use String#to_json and</span>
      <span class="c1"># Fixnum#to_json as part of the definition of Array#to_json.</span>
      <span class="s2">"["</span> <span class="o">+</span> <span class="n">map</span> <span class="p">{</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="n">x</span><span class="p">.</span><span class="nf">to_json</span> <span class="p">}</span> <span class="o">+</span> <span class="s2">"]"</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>If you don’t do anything other than that, you won’t notice anything at all. However, now you can choose to use this refinement at many different scopes:</p>

<div><span class="n">using</span> <span class="no">JSONGenerator</span>       <span class="c1"># For the whole file</span>
<span class="mi">1</span><span class="p">.</span><span class="nf">to_json</span>

<span class="k">module</span> <span class="nn">Application</span>
  <span class="n">using</span> <span class="no">JSONGenerator</span>     <span class="c1"># For this module and any nested classes and modules</span>
                          <span class="c1"># E.g. this also applies to Application::Controller</span>
  
  <span class="c1"># It works directly inside the class definition:</span>
  <span class="mi">2</span><span class="p">.</span><span class="nf">to_json</span>

  <span class="c1"># And inside methods:</span>
  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">hello</span>
    <span class="mi">3</span><span class="p">.</span><span class="nf">to_json</span>
  <span class="k">end</span>
  
  <span class="k">class</span> <span class="nc">Controllers</span>
    <span class="n">using</span> <span class="no">JSONGenerator</span>   <span class="c1"># For this class and any nested classes and modules</span>
    
    <span class="k">def</span> <span class="nf">get</span>
      <span class="n">using</span> <span class="no">JSONGenerator</span> <span class="c1"># For this method only</span>
      <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">].</span><span class="nf">to_json</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>The great thing about refinements, is that it’s technically impossible to globally leak them. They will always be restricted to the scope you specify, and there’s nothing “above” the file scope.</p>

<p>That’s not <em>always</em> true though. Refinements are also enabled in subclasses and reopened classes, even if they are located in different files.</p>

<div><span class="k">class</span> <span class="nc">ApplicationController</span>
  <span class="n">using</span> <span class="no">JSONGenerator</span>
<span class="k">end</span>

<span class="c1"># Somewhere else:</span>
<span class="k">class</span> <span class="nc">ApplicationController</span>
  <span class="nb">p</span> <span class="mi">123</span><span class="p">.</span><span class="nf">to_json</span>  <span class="c1"># Still works</span>
<span class="k">end</span>

<span class="k">class</span> <span class="nc">UsersController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
  <span class="nb">p</span> <span class="mi">123</span><span class="p">.</span><span class="nf">to_json</span>  <span class="c1"># Still works</span>
<span class="k">end</span>

<span class="nb">p</span> <span class="mi">123</span><span class="p">.</span><span class="nf">to_json</span>  <span class="c1"># This doesn&#39;t work however</span></div>

<p>But here comes the best part: Refinements are also carried on in class_eval, module_eval and instance_eval:</p>

<div><span class="k">module</span> <span class="nn">Expectations</span>
  <span class="n">refine</span> <span class="no">Object</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nf">should</span><span class="p">;</span> <span class="o">...</span> <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="k">def</span> <span class="nf">it</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span>
  <span class="c1"># Remember that refinements can see one another:</span>
  <span class="no">Expectations</span><span class="p">.</span><span class="nf">module_eval</span><span class="p">(</span><span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span>
<span class="k">end</span>

<span class="n">it</span> <span class="s2">"should be awesome"</span> <span class="k">do</span>
  <span class="ss">:refinements</span><span class="p">.</span><span class="nf">level</span><span class="p">.</span><span class="nf">should</span> <span class="o">==</span> <span class="ss">:awesome</span>
<span class="k">end</span></div>

<p>Holy Schmoly, now we’re talking! Even this works as expected:</p>

<div><span class="k">class</span> <span class="nc">TestScope</span>
  <span class="n">using</span> <span class="no">Expectations</span>
  <span class="nb">attr_reader</span> <span class="ss">:msg</span>

  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
    <span class="vi">@msg</span> <span class="o">=</span> <span class="n">msg</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="k">def</span> <span class="nf">it</span><span class="p">(</span><span class="n">msg</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span>
  <span class="no">TestScope</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">msg</span><span class="p">).</span><span class="nf">instance_eval</span><span class="p">(</span><span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span>
<span class="k">end</span></div>

<p>Refinements are also inherited, so Rails 4 could provide this module:</p>

<div><span class="k">module</span> <span class="nn">ActiveSupport::All</span>
  <span class="n">using</span> <span class="no">ActiveSupport</span><span class="o">::</span><span class="no">Autoload</span>
  <span class="n">using</span> <span class="no">ActiveSupport</span><span class="o">::</span><span class="no">Callbacks</span>
  <span class="c1"># and so on ...</span>
<span class="k">end</span></div>

<p>And because refinements are also enabled in subclasses:</p>

<div><span class="k">class</span> <span class="nc">ApplicationController</span> <span class="o">&lt;</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">Base</span>
  <span class="n">using</span> <span class="no">ActiveSupport</span><span class="o">::</span><span class="no">All</span>
<span class="k">end</span>

<span class="k">class</span> <span class="nc">ArticlesController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
  <span class="k">def</span> <span class="nf">index</span>
    <span class="vi">@articles</span> <span class="o">=</span> <span class="no">Article</span><span class="p">.</span><span class="nf">where</span><span class="p">(</span><span class="s2">"created_at &gt; ?"</span><span class="p">,</span> <span class="mi">3</span><span class="p">.</span><span class="nf">days</span><span class="p">.</span><span class="nf">ago</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>You can continue developing <strong>in the exact same way</strong> as before, but now without leaking anything into the global namespace.</p>

<p><em>(This is the moment where you’re proposing to marry Shugo.)</em></p>

<h2 id="whats_the_catch">What’s the catch?</h2>

<p>There’s a few things you need to be aware of. First of all, there might be a little decrease in performance. Hopefully this will be resolved (or turn out to be insignificant) in the future. Other issues:</p>

<h3 id="include_and_using_are_completely_separated">#include and #using are completely separated:</h3>

<div><span class="k">module</span> <span class="nn">Rack::Utils</span>
  <span class="n">refine</span> <span class="no">Object</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nf">call</span><span class="p">;</span> <span class="o">...</span> <span class="k">end</span>
  <span class="k">end</span>
  
  <span class="k">def</span> <span class="nf">escape_html</span><span class="p">;</span> <span class="o">...</span> <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="c1"># I want use both:</span>
<span class="k">module</span> <span class="nn">Camping</span>
  <span class="kp">include</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Utils</span>
  <span class="n">using</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Utils</span>
<span class="k">end</span></div>

<p>Because refinements are lexically scoped, it’s also not possible to combine them with an included hook:</p>

<div><span class="k">module</span> <span class="nn">Rack::Utils</span>
  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">included</span><span class="p">(</span><span class="n">mod</span><span class="p">)</span>
    <span class="c1"># Doesn&#39;t work as expected:</span>
    <span class="n">mod</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="ss">:using</span><span class="p">,</span> <span class="nb">self</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="k">module</span> <span class="nn">Camping</span>
  <span class="kp">include</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Utils</span>
<span class="k">end</span></div>

<p>You can however use the <em>used</em> hook:</p>

<div><span class="k">module</span> <span class="nn">Rack::Utils</span>
  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">used</span><span class="p">(</span><span class="n">mod</span><span class="p">)</span>
    <span class="n">mod</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="ss">:include</span><span class="p">,</span> <span class="nb">self</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="k">module</span> <span class="nn">Camping</span>
  <span class="n">using</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Utils</span>
<span class="k">end</span></div>

<h3 id="singleton_methods_in_refinements_are_not_included">Singleton methods in refinements are not included:</h3>

<div><span class="k">module</span> <span class="nn">FixnumExt</span>
  <span class="c1"># This has no effect:</span>
  <span class="n">refine</span> <span class="no">Fixnum</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">thing</span><span class="p">;</span> <span class="o">...</span> <span class="k">end</span>
  <span class="k">end</span>
  
  <span class="c1"># Use this instead:</span>
  <span class="n">refine</span> <span class="no">Fixnum</span><span class="p">.</span><span class="nf">singleton_class</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nf">thing</span><span class="p">;</span> <span class="o">...</span> <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<h3 id="you_cant_refine_modules">You can’t refine modules:</h3>

<div><span class="k">module</span> <span class="nn">EnumerableExt</span>
  <span class="c1"># Error:</span>
  <span class="n">refine</span> <span class="no">Enumerable</span> <span class="k">do</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<h3 id="refinements_dont_have_local_rebinding">Refinements don’t have local rebinding</h3>

<p>Another important fact is that, unlike classboxes, refinements don’t have <em>local rebinding</em>. Let me show you an example:</p>

<div><span class="k">class</span> <span class="nc">CharArray</span>
  <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">str</span><span class="p">)</span>
    <span class="vi">@array</span> <span class="o">=</span> <span class="n">str</span><span class="p">.</span><span class="nf">unpack</span><span class="p">(</span><span class="s2">"C*"</span><span class="p">)</span>  <span class="c1"># Unpacks to integers</span>
  <span class="k">end</span>
  
  <span class="k">def</span> <span class="nf">each</span><span class="p">(</span><span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span>
    <span class="vi">@array</span><span class="p">.</span><span class="nf">each</span><span class="p">(</span><span class="o">&amp;</span><span class="n">blk</span><span class="p">)</span>
  <span class="k">end</span>
  
  <span class="k">def</span> <span class="nf">print_each</span>
    <span class="n">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">chr</span><span class="o">|</span> <span class="nb">p</span> <span class="n">chr</span> <span class="p">}</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="nb">test</span> <span class="o">=</span> <span class="no">CharArray</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"Hello World"</span><span class="p">)</span>
<span class="nb">test</span><span class="p">.</span><span class="nf">print_each</span>   <span class="c1"># Prints a list of integers (expected)</span>

<span class="c1"># A refinement which overwrites CharArray#each to return one-char strings</span>
<span class="c1"># instead of integers:</span>
<span class="k">module</span> <span class="nn">CharArrayStr</span>
  <span class="n">refine</span> <span class="no">CharArray</span> <span class="k">do</span>
    <span class="k">def</span> <span class="nf">each</span>
      <span class="k">super</span> <span class="p">{</span> <span class="o">|</span><span class="n">c</span><span class="o">|</span> <span class="k">yield</span> <span class="n">c</span><span class="p">.</span><span class="nf">chr</span> <span class="p">}</span>
    <span class="k">end</span>
  <span class="k">end</span>
<span class="k">end</span>

<span class="n">using</span> <span class="no">CharArrayStr</span>
<span class="nb">test</span><span class="p">.</span><span class="nf">each</span> <span class="p">{</span> <span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="nb">p</span> <span class="n">x</span> <span class="p">}</span>  <span class="c1"># Prints a list of strings</span>
<span class="nb">test</span><span class="p">.</span><span class="nf">print_each</span>        <span class="c1"># Prints a list of integers?!</span></div>

<p>At first, it might seem counter-intuitive. Why does the last line prints a list of integers? Why isn’t the refinement enabled in that method? As you might have guessed, it’s because refinements don’t have <em>local rebinding</em>. This means that the refinements will <em>only</em> apply to the scope they are enabled. The moment you call a method <em>outside</em> of the scope, none of the refinements apply anymore.</p>

<p>The advantage of this is that you can safely override methods without thinking about breaking anything else. You simply can’t refine code in another scope. However, there’s a huge disadvantage: If there’s a “core” method (like #each above) which is used by several other methods, you can’t affect the other methods.</p>

<p>Local rebinding might be implemented, but in that case, refinements will be renamed to classbox (since that’s what they are).</p>

<h2 id="current_status">Current status</h2>

<p>As I’ve mentioned earlier, there is currently <a href="http://stuff.judofyr.net/refinements.diff">a patch</a> available that builds cleanly on top of r29837. There are some implementation details which might need to be resolved, but as far as I know, <strong>both matz and ko1 are positive for merging the patch.</strong></p>

<h2 id="resources">Resources</h2>

<ul>
<li><a href="http://www.slideshare.net/ShugoMaeda/rc2010-refinements">The slides to Shugo’s talk</a></li>

<li><a href="http://redmine.ruby-lang.org/issues/show/4085">The proposal at ruby-core</a></li>
</ul>

<p>Refinements are very much a work in progress, so if you have any more details or questions about how they work, feel free to contact me on <a href="mailto:&#116;&#105;&#109;&#101;&#108;&#101;&#115;&#115;&#064;&#106;&#117;&#100;&#111;&#102;&#121;&#114;&#046;&#110;&#101;&#116;">&#116;&#105;&#109;&#101;&#108;&#101;&#115;&#115;&#064;&#106;&#117;&#100;&#111;&#102;&#121;&#114;&#046;&#110;&#101;&#116;</a> so I can keep this article up to date.</p>

<p>(Thanks to Shugo Maeda for explaining in detail how refinements work, and <a href="http://runerb.com/">Rune Botten</a> and <a href="http://ithaca.arpinum.org/">Peter Aronoff</a> for reading drafts of this.)</p>
]]></content>
  </entry>
  <entry>
    <title>Updated: Tailin' Ruby</title>
    <id>tag:timeless.judofyr.net,2010-11-24:1290607523</id>
    <link href="http://timelessrepo.com/tailin-ruby"/>
    <updated>2010-11-24T14:05:23Z</updated>
    <published>2010-11-24T14:05:23Z</published>
    <content type="html"><![CDATA[
<p>Roman Semenenko reported in that there’s <em>yet</em> another way to use tail-call optimizations in Ruby. I’ve updated the article with some details on how to use it, and also updated the benchmark so you can easily compare the performance between the different methods.</p>
<p><a href="http://timelessrepo.com/tailin-ruby">Continue to updated post.</a></p>]]></content>
  </entry>
  <entry>
    <title>Block Helpers in Rails 3</title>
    <id>tag:timeless.judofyr.net,2010-11-21:1290346885</id>
    <link href="http://timelessrepo.com/block-helpers-in-rails3"/>
    <updated>2010-11-21T13:41:25Z</updated>
    <published>2010-11-21T13:41:25Z</published>
    <content type="html"><![CDATA[
<p>In May 2010 I attended <a href="http://frozenrails.eu/">Frozen Rails</a> in Helsinki where Carl Lerche held a talk about Rails 3. Like everything else regarding Rails 3 it wasn’t a <em>revolutionary</em> talk, but I rather found myself nodding “Yep, that’s a better solution” every now and then (which after all is the whole idea of Rails 3).</p>

<p>When it comes to block helpers in ERB however, I wasn’t quite so positive. Block helpers have always been a little confusing in Rails - both their usage and their implementation. Rails 3 attempts to clean them up, but at what cost?</p>

<h2 id="rails_23">Rails 2.3</h2>

<p>This shows the regular usage of block helpers in Rails 2.3:</p>

<div><span class="cp">&lt;%</span> <span class="n">form_for</span> <span class="vi">@post</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span> <span class="cp">%&gt;</span>
  <span class="cp">&lt;%=</span> <span class="n">f</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:title</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>

<span class="cp">&lt;%</span> <span class="n">box</span> <span class="k">do</span> <span class="cp">%&gt;</span>
  <span class="nt">&lt;p&gt;</span>Hello World!<span class="nt">&lt;/p&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span></div>

<p>It’s what we’re used to and it probably seems completely fine to most of us, but when you think about it, it’s not really consistent. Both the form_for and the f.text_field output HTML, but only the latter uses ERB’s output syntax. The block helper magically outputs the content itself:</p>

<div><span class="k">def</span> <span class="nf">box</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
  <span class="n">content</span> <span class="o">=</span> <span class="s2">"&lt;div class=&#39;box&#39;&gt;"</span> <span class="o">+</span> <span class="n">capture</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"&lt;/div&gt;"</span>
  
  <span class="k">if</span> <span class="n">block_called_from_erb?</span><span class="p">(</span><span class="n">block</span><span class="p">)</span>
    <span class="n">concat</span><span class="p">(</span><span class="n">content</span><span class="p">)</span>
  <span class="k">else</span>
    <span class="n">content</span>
  <span class="k">end</span>
<span class="k">end</span></div>

<p>Notice the <code>block_called_from_erb?(block)</code>. If the method was called inside of ERB, it automagically concats the string to the result. If box is called somewhere else, it just returns the string. There’s tons of these checks in Rails 2.3, and they’re not pretty (especially not the implementation of block_called_from_erb?)</p>

<h2 id="rails_3">Rails 3</h2>

<p>Here’s the same example in Rails 3:</p>

<div><span class="cp">&lt;%=</span> <span class="n">form_for</span> <span class="vi">@post</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span> <span class="cp">%&gt;</span>
  <span class="cp">&lt;%=</span> <span class="n">f</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:title</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>

<span class="cp">&lt;%=</span> <span class="n">box</span> <span class="k">do</span> <span class="cp">%&gt;</span>
  <span class="nt">&lt;p&gt;</span>Hello World!<span class="nt">&lt;/p&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span></div>

<p>Notice how it’s consistent with ERB. Both form_for and f.text_field output HTML, and both of them uses ERB’s output syntax. Therefore, the implementation is much simpler:</p>

<div><span class="k">def</span> <span class="nf">box</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span>
  <span class="s2">"&lt;div class=&#39;box&#39;&gt;"</span> <span class="o">+</span> <span class="n">capture</span><span class="p">(</span><span class="o">&amp;</span><span class="n">block</span><span class="p">)</span> <span class="o">+</span> <span class="s2">"&lt;/div&gt;"</span>
<span class="k">end</span></div>

<p>Everything is much simpler and prettier. I bet you’re wondering: “Why so negative?”</p>

<h2 id="its_no_longer_erb">It’s no longer ERB</h2>

<p>It’s no longer ERB. When you use this <em>extension</em>, you’re not writing ERB anymore. You’re writing Rails ERB. <code>&lt;%= box do %&gt;</code> is simply not valid ERB. While there’s no written spec for ERB, there are some basic rules which every implementation of ERB can follow.</p>

<p>The first rule is that the expression in <code>&lt;%= =%&gt;</code> must be a <em>complete expression</em> <sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>. A complete expression is an expression which you can pass directly into eval without getting a syntax error. Or you could say that it’s a piece of Ruby code which you can place <em>parenthesis</em> around and it still ends up as valid Ruby:</p>

<div><span class="nb">eval</span><span class="p">(</span><span class="s2">"f.text_field"</span><span class="p">)</span>  <span class="c1"># =&gt; Works fine</span>
<span class="p">(</span> <span class="n">f</span><span class="p">.</span><span class="nf">text_field</span> <span class="p">)</span>      <span class="c1"># =&gt; Valid</span>

<span class="nb">eval</span><span class="p">(</span><span class="s2">"box do"</span><span class="p">)</span>        <span class="c1"># =&gt; SyntaxError</span>
<span class="p">(</span> <span class="n">box</span> <span class="k">do</span> <span class="p">)</span>            <span class="c1"># =&gt; Invalid</span></div>

<p>The expression in <code>&lt;% %&gt;</code> on the other hand only needs to be a <em>subexpression</em>. A subexpression is something which by itself is an invalid expression, but becomes valid if there’s another subexpression below or above it which completes it. You could also say that it’s a piece of Ruby code which you can place <em>semicolons</em> around:</p>

<pre><code>eval(&quot;box do&quot;)    # =&gt; SyntaxError
; box do ;        # =&gt; Valid (as long as there is an `end` later)</code></pre>

<p>As you can see, <code>box do</code> is a subexpression, but not a complete expression. Therefore, in normal ERB, you can put it inside <code>&lt;% %&gt;</code>, but not <code>&lt;%= %&gt;</code>.</p>

<h2 id="why_is_there_a_difference">Why is there a difference?</h2>

<p>Now you’re probably wondering <em>why</em> there is a difference between <code>&lt;%=
%&gt;</code> and <code>&lt;% %&gt;</code>. Couldn’t just both accept a subexpression? In order to understand this issue, we’ll have to look at how ERB generates code.</p>

<p>Given this code:</p>

<div><span class="cp">&lt;%</span> <span class="n">form_for</span> <span class="vi">@post</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span> <span class="cp">%&gt;</span>
  <span class="cp">&lt;%=</span> <span class="n">f</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:title</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span>

<span class="cp">&lt;%</span> <span class="n">box</span> <span class="k">do</span> <span class="cp">%&gt;</span>
  <span class="nt">&lt;p&gt;</span>Hello World!<span class="nt">&lt;/p&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span></div>

<p>ERB generates this code:</p>

<div><span class="n">_buffer</span> <span class="o">=</span> <span class="s2">""</span>
<span class="n">form_for</span> <span class="vi">@post</span> <span class="k">do</span> <span class="o">|</span><span class="n">f</span><span class="o">|</span>
  <span class="n">_buffer</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="n">f</span><span class="p">.</span><span class="nf">text_field</span> <span class="ss">:title</span><span class="p">).</span><span class="nf">to_s</span>
<span class="k">end</span>

<span class="n">box</span> <span class="k">do</span>
  <span class="n">_buffer</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="s2">"&lt;p&gt;Hello World!"</span><span class="p">).</span><span class="nf">to_s</span>
<span class="k">end</span></div>

<p>As you can see, it needs to convert the expressions to strings in order to concat it to the buffer. It’s not always about converting to strings though; in some cases you might for instance want to wrap it within another method call (like <code>CGI.escapeHTML</code> to prevent XSS attacks).</p>

<p>It’s not technically possible to do this with <em>all</em> subexpressions:</p>

<div><span class="n">_buffer</span> <span class="o">&lt;&lt;</span> <span class="p">(</span> <span class="n">box</span> <span class="k">do</span> <span class="p">).</span><span class="nf">to_s</span>
  <span class="n">_buffer</span> <span class="o">&lt;&lt;</span> <span class="p">(</span><span class="s2">"&lt;p&gt;Hello World!&lt;/p&gt;"</span><span class="p">).</span><span class="nf">to_s</span>
<span class="k">end</span></div>

<h2 id="impossible_but_it_works_in_rails_3">Impossible? But it works in Rails 3!</h2>

<p>Let’s have a look at how it’s implemented in Rails 3:</p>

<div><span class="c1"># in actionpack/lib/action_view/template/handlers/erb.rb</span>
<span class="k">class</span> <span class="nc">ActionView</span><span class="o">::</span><span class="no">Template</span><span class="o">::</span><span class="no">Handlers</span><span class="o">::</span><span class="no">Erubis</span> <span class="o">&lt;</span> <span class="no">Erubis</span><span class="o">::</span><span class="no">Eruby</span>
  <span class="no">BLOCK_EXPR</span> <span class="o">=</span> <span class="sr">/\s+(do|\{)(\s*\|[^|]*\|)?\s*\Z/</span>

  <span class="k">def</span> <span class="nf">add_expr_literal</span><span class="p">(</span><span class="n">src</span><span class="p">,</span> <span class="n">code</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">code</span> <span class="o">=~</span> <span class="no">BLOCK_EXPR</span>
      <span class="n">src</span> <span class="o">&lt;&lt;</span> <span class="s1">&#39;@output_buffer.append= &#39;</span> <span class="o">&lt;&lt;</span> <span class="n">code</span>
    <span class="k">else</span>
      <span class="n">src</span> <span class="o">&lt;&lt;</span> <span class="s1">&#39;@output_buffer.append= (&#39;</span> <span class="o">&lt;&lt;</span> <span class="n">code</span> <span class="o">&lt;&lt;</span> <span class="s1">&#39;);&#39;</span>
    <span class="k">end</span>
  <span class="k">end</span></div>

<p>It’s basically one big hack. It uses a <em>regular expression</em> for figuring out whether or not an expression is a block expression. Then it has to use a fake write attribute (<code>alias :append= :&lt;&lt;</code>) so it doesn’t need to wrap the code in parenthesis.</p>

<p>One big, fragile hack which breaks easily:</p>

<div># A valid Ruby expression, but invalid Rails ERB.
<span class="cp">&lt;%=</span> <span class="n">article</span> <span class="k">do</span> <span class="o">|</span><span class="n">a</span><span class="o">|</span> <span class="n">m</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="nf">metadata</span> <span class="cp">%&gt;</span>
  Written by: <span class="cp">&lt;%=</span> <span class="n">m</span><span class="p">[</span><span class="ss">:author</span><span class="p">]</span> <span class="cp">%&gt;</span>
  <span class="cp">&lt;%=</span> <span class="n">a</span><span class="p">.</span><span class="nf">content</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span></div>

<h2 id="conclusion">Conclusion</h2>

<p><strong>Yes</strong>, I agree that block helpers are ugly in Rails 2.3.</p>

<p><strong>No</strong>, In Rails 3 you’re not writing ERB; you’re writing Rails ERB, RERB, ERB-with-funky-hacky-block-helpers-thingie or whatever you want to call it. It’s still not ERB.</p>

<p>The fact that Rails still claims it uses ERB is what makes me uncomfortable. They’re silently chainging the behaviour of ERB, but currently it only sort-of works in a single framework (Rails) with a single ERB implementation (Erubis).</p>

<p>Just because ERB itself isn’t formally spec’ed somewhere, doesn’t mean you can modify it your own needs and still claim it to be ERB. I fear that we’re ending up with two “versions” of ERB which will be confusing for both those writing the ERB templates, those who want to include ERB support and those who maintains an ERB implementation.</p>
<div class="footnotes"><hr /><ol><li id="fn:1">
<p>The reason you haven’t heard about a complete expression before is because I came up with the term. Please <a href="/comments">let me know</a> if you have a better suggestion or know the “official” name for it. <a href="#fnref:1" rev="footnote">↩</a></p>
</li></ol></div>]]></content>
  </entry>
  <entry>
    <title>Timeless</title>
    <id>tag:timeless.judofyr.net,2010-10-03:1286119149</id>
    <link href="http://timelessrepo.com/timeless"/>
    <updated>2010-10-03T15:19:09Z</updated>
    <published>2010-10-03T15:19:09Z</published>
    <content type="html"><![CDATA[
<p>Welcome to Timeless, a community blog with focus on <em>content</em>.</p>

<p>I started my first technical blog in Feburary 2008 and have been blogging infrequently since then. As time has passed by, I’ve discovered that a blog is a surprisingly <em>bad</em> way to present technical content, both for the author and the reader. Timeless is an attempt at creating a new kind of blog with a focus on frequently updated, quality content which lasts longer than the beta of your favourite framework.</p>

<h2 id="the_meaning_of_a_blog_post">The Meaning of a Blog Post</h2>

<p>Attach a timestamp to a piece of text. Present it in reverse-chronological order. Simple and effective. For almost a decade blogs have been the primary medium for inviduals to express their opinion on the internet. A blog is perfect for writing about <em>changes</em>: a person’s life, the society in general, the progress of a project, or other events. The readers are encouraged to frequently visit the blog in order to keep up to date, and in some blogs (these days: <em>most</em> blogs) they can also post comments and take a part of the discussion.</p>

<p>So simple and so effective that it has slowly taken over the internet, which isn’t <em>only</em> positive. Useful information is hidden beneath “September 2008”, but is still as relevant today. Posts with glaring mistakes are left online because all blog posts should be immutable. People forget to write down the context (in the technical world: version numbers, operation systems etc), so it’s impossible to know if the post still applies a few years later.</p>

<p>The blog has become the norm, even in the places where it doesn’t fit <em>at all</em>.</p>

<h2 id="a_blog_that_isnt_a_blog">A blog that isn’t a blog</h2>

<p>You might have arrived at this site under the impression that this is a blog. If so, I’m sorry to disappoint you: you won’t find any timestamps here. Why? Because we don’t want to write about changes. We don’t have what it takes to maintain a blog and post regularly. All we want to do is to <em>write</em>. Not often, not always, but still: there are times when we simply want to write.</p>

<p>If we’re not going to take advantage of the blog medium, then why should we be impeded by the disadvantages? Why do I feel I’m <em>obliged</em> to add a timestamp to each post? Why do I feel I’m <em>not allowed</em> to change or update the posts?</p>

<p>I feel brainwashed, and Timeless is my attempt at breaking out of the prison.</p>

<h2 id="content_not_changes">Content, not changes</h2>

<p>The goal of Timeless is that every article should be <em>timeless</em>. Well, not timeless in the usual meaning of the word, but timeless as in every article should include enough context to be easily understandable if you discover it a month, a year or maybe even ten years later. The article should be regularly updated as we learn and discover new aspects of the topic.</p>

<p>Just because Timeless focuses on content, doesn’t mean there’s no focus on the changes. I don’t expect people to manually look for changes, so there’s both an <a href="http://feeds.feedburner.com/TimelessRepo">RSS feed</a> and <a href="/changelog">a separate page</a> about the recent changes at Timeless.</p>

<p>This gives us the perfect flexibilty: New readers can browser and explore the content like it’s a wiki, while returning readers can easily see if there’s any updated content. If you want to be notifed about further updates, you can simply subscribe to the feed, just like a normal blog.</p>

<h2 id="you_can_help_too">You can help too!</h2>

<p>Nothing about Timeless is limited to <em>one</em> author (in fact, we are two authors writing regulary now) and as long as you have something interesting to say, you are more than welcome to <a href="/contribute">help us make Timeless even better</a>.</p>

<h2 id="this_is_just_the_beginning">This is just the beginning…</h2>

<p>What you are seeing here is only the first version of Timeless. It’s far from complete, but definitely <em>good enough</em> for now. In the future I hope to add more features (like tags, sub-articles and maybe even comments), but there’s no point of implementing something before we actually need it.</p>

<p>If you’re still interested in Timeless, you can <a href="http://feeds.feedburner.com/TimelessRepo">subscribe to the feed</a> or follow the project at <a href="http://github.com/judofyr/timeless">GitHub</a>.</p>

<p>Please let me know if you <a href="/comments">have any comments, thoughts or questions</a>.</p>
]]></content>
  </entry>
  <entry>
    <title>Where's the rest?</title>
    <id>tag:timeless.judofyr.net,2010-10-03:1286119149</id>
    <link href="http://timelessrepo.com/changelog/1"/>
    <updated>2010-10-03T15:19:09Z</updated>
    <published>2010-10-03T15:19:09Z</published>
    <content type="html"><![CDATA[
<p>Since I’ve imported all the articles from my previous blog, there aren’t more changes here. There’s still of plenty of content though: <a href="/#index">Check out all the articles at the front page</a>.</p>
]]></content>
  </entry>
</feed>

