<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Matt Aimonetti]]></title>
  <link href="http://matt.aimonetti.net/atom.xml" rel="self"/>
  <link href="http://matt.aimonetti.net/"/>
  <updated>2013-03-09T12:09:14-08:00</updated>
  <id>http://matt.aimonetti.net/</id>
  <author>
    <name><![CDATA[Matt Aimonetti]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Inspecting Rails 4 using Ruby 2.0]]></title>
    <link href="http://matt.aimonetti.net/posts/2013/03/05/inspecting-rails-4-request-dispatch-using-ruby-2-dot-0/"/>
    <updated>2013-03-05T22:18:00-08:00</updated>
    <id>http://matt.aimonetti.net/posts/2013/03/05/inspecting-rails-4-request-dispatch-using-ruby-2-dot-0</id>
    <content type="html"><![CDATA[<p>Ruby 2.0 has a cool new feature that many people talk about:
<a href="http://ruby-doc.org/core-2.0/TracePoint.html">TracePoint</a>.</p>

<p><code>TracePoint</code> essentially allows you to hook into Ruby&#8217;s events and
listen for events.</p>

<p>Being curious and since I just started a brand new Rails 4/Ruby 2 app, I
decided to write a little middleware and see what Rails is up to when
handling incoming requests.</p>

<p>Here is my <a href="https://gist.github.com/mattetti/5097206">TracePoint Rack Middleware</a>.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">TracePoint</span>
</span><span class='line'>  <span class="k">class</span> <span class="nc">Middleware</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
</span><span class='line'>      <span class="vi">@app</span> <span class="o">=</span> <span class="n">app</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">def</span> <span class="nf">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
</span><span class='line'>      <span class="n">stats</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>      <span class="n">trace</span> <span class="o">=</span> <span class="no">TracePoint</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="ss">:call</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">tp</span><span class="o">|</span>
</span><span class='line'>        <span class="n">stats</span><span class="o">[</span><span class="n">tp</span><span class="o">.</span><span class="n">defined_class</span><span class="o">]</span> <span class="o">||=</span> <span class="p">{}</span>
</span><span class='line'>        <span class="n">stats</span><span class="o">[</span><span class="n">tp</span><span class="o">.</span><span class="n">defined_class</span><span class="o">][</span><span class="n">tp</span><span class="o">.</span><span class="n">method_id</span><span class="o">]</span> <span class="o">||=</span> <span class="mi">0</span>
</span><span class='line'>        <span class="n">stats</span><span class="o">[</span><span class="n">tp</span><span class="o">.</span><span class="n">defined_class</span><span class="o">][</span><span class="n">tp</span><span class="o">.</span><span class="n">method_id</span><span class="o">]</span> <span class="o">+=</span> <span class="mi">1</span>
</span><span class='line'>      <span class="k">end</span>
</span><span class='line'>      <span class="n">trace</span><span class="o">.</span><span class="n">enable</span>
</span><span class='line'>      <span class="n">response</span> <span class="o">=</span> <span class="vi">@app</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
</span><span class='line'>      <span class="n">trace</span><span class="o">.</span><span class="n">disable</span>
</span><span class='line'>
</span><span class='line'>      <span class="nb">puts</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">stats</span><span class="o">.</span><span class="n">keys</span><span class="o">.</span><span class="n">size</span><span class="si">}</span><span class="s2"> classes used&quot;</span>
</span><span class='line'>      <span class="nb">puts</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">stats</span><span class="o">.</span><span class="n">map</span><span class="p">{</span><span class="o">|</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="o">|</span> <span class="n">v</span><span class="o">.</span><span class="n">keys</span><span class="si">}</span><span class="s2">.flatten.size} methods used&quot;</span>
</span><span class='line'>      <span class="nb">puts</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">stats</span><span class="o">.</span><span class="n">map</span><span class="p">{</span><span class="o">|</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="o">|</span> <span class="n">v</span><span class="o">.</span><span class="n">values</span><span class="si">}</span><span class="s2">.flatten.sum} methods dispatched&quot;</span>
</span><span class='line'>      <span class="n">response</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>(the gist shows a modified version so I could dump to disk the json
representation of the calls)</p>

<p>I then inserted the middleware in Rails:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># in application.rb</span>
</span><span class='line'><span class="n">config</span><span class="o">.</span><span class="n">middleware</span><span class="o">.</span><span class="n">insert_before</span><span class="p">(</span><span class="no">ActionDispatch</span><span class="o">::</span><span class="no">Static</span><span class="p">,</span> <span class="no">TracePoint</span><span class="o">::</span><span class="no">Middleware</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>I saved the output in json format for the curious: <a href="https://gist.github.com/mattetti/5097178">click here</a></p>

<p>On average, in production mode, using Ruby 2.0 and Puma on my laptop, my hello world index page takes 5ms.</p>

<p>To render my page, Rails uses (more or less):</p>

<ul>
<li>250 classes</li>
<li>750 methods (not including C functions)</li>
<li>and dispatches 2704 methods (not including calls to C functions)</li>
</ul>


<p>Here is a small selection of some of the methods dispatched:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='json'><span class='line'><span class="s2">&quot;String&quot;</span><span class="err">:</span> <span class="p">{</span>
</span><span class='line'>    <span class="nt">&quot;underscore&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;blank?&quot;</span><span class="p">:</span> <span class="mi">14</span><span class="p">,</span>
</span><span class='line'>    <span class="nt">&quot;html_safe&quot;</span><span class="p">:</span> <span class="mi">78</span>
</span><span class='line'>  <span class="p">}</span><span class="err">,</span>
</span><span class='line'><span class="s2">&quot;ActiveSupport::Inflector&quot;</span><span class="err">:</span> <span class="p">{</span>
</span><span class='line'>  <span class="nt">&quot;underscore&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;inflections&quot;</span><span class="p">:</span> <span class="mi">2</span>
</span><span class='line'><span class="p">}</span><span class="err">,</span>
</span><span class='line'><span class="s2">&quot;Hash&quot;</span><span class="err">:</span> <span class="p">{</span>
</span><span class='line'>  <span class="nt">&quot;with_indifferent_access&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;except&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;except!&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;stringify_keys&quot;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;transform_keys&quot;</span><span class="p">:</span> <span class="mi">13</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;stringify_keys!&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;transform_keys!&quot;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;extractable_options?&quot;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;extract!&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;symbolize_keys&quot;</span><span class="p">:</span> <span class="mi">8</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;reverse_merge&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;slice&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;symbolize_keys!&quot;</span><span class="p">:</span> <span class="mi">2</span>
</span><span class='line'><span class="p">}</span><span class="err">,</span>
</span><span class='line'><span class="s2">&quot;ActionView::CompiledTemplates&quot;</span><span class="err">:</span> <span class="p">{</span>
</span><span class='line'>  <span class="nt">&quot;_app_views_welcome_index_html_erb__4177595130715791755_70209827438920&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;_app_views_layouts_application_html_erb___652124533295419796_70209827456500&quot;</span><span class="p">:</span> <span class="mi">1</span>
</span><span class='line'><span class="p">}</span><span class="err">,</span>
</span><span class='line'><span class="s2">&quot;ActiveSupport::Notifications::Fanout&quot;</span><span class="err">:</span> <span class="p">{</span>
</span><span class='line'>  <span class="nt">&quot;start&quot;</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;listeners_for&quot;</span><span class="p">:</span> <span class="mi">12</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;listening?&quot;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span>
</span><span class='line'>  <span class="nt">&quot;finish&quot;</span><span class="p">:</span> <span class="mi">3</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p><code>TracePoint</code> is a great new addition and I hope to see some new crazy
tools being developed (production dead-code analyzer, deprecation code path
finder anyone?)</p>

<p>To end, this short post, here is an interesting quote from <a href="http://chadfowler.com/">Chad
Fowler</a> Berliner by adoption:</p>

<blockquote><p>Abstractions are expensive. The cost increases exponentially as you add them to a codebase.
<a href="https://twitter.com/chadfowler/status/308959527217270786">Chad Fowler</a></p></blockquote>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[OmniAuth and Google Apps]]></title>
    <link href="http://matt.aimonetti.net/posts/2013/01/30/omniauth-and-google-apps/"/>
    <updated>2013-01-30T19:11:00-08:00</updated>
    <id>http://matt.aimonetti.net/posts/2013/01/30/omniauth-and-google-apps</id>
    <content type="html"><![CDATA[<p>Today I struggled to get <a href="https://github.com/intridea/omniauth">OmniAuth</a> and <a href="https://developers.google.com/accounts/docs/OpenID">Google apps</a> to work properly together.
I just wanted to add authentication to my application and restrict access to only my Google Apps domain users.
I was hoping it would be straight forward since I could use Google&#8217;s OpenID service.</p>

<p>Turns out it wasn&#8217;t that hard, but the lack of documentation made me
lost a couple hours.
I therefore updated <a href="https://github.com/intridea/omniauth/wiki">OmniAuth&#8217;s wiki</a> and wrote this quick post so hopefully you won&#8217;t waste time looking for simple details.</p>

<h2>Requirements</h2>

<p>You actually only need to add 2 gems to your Gemfile:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">gem</span> <span class="s1">&#39;omniauth-openid&#39;</span>
</span><span class='line'><span class="n">gem</span> <span class="s1">&#39;ruby-openid-apps-discovery&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now, the second gem is the one I didn&#8217;t know about.
The gem is actually provided by <a href="https://github.com/google/ruby-openid-apps-discovery">Google itself</a>. It turns out, Google Apps use a custom discovery protocol.
They monkey patched the popular OpenID Ruby libraries so you can just drop in
their gem and their discovery system will magically work.</p>

<h2>Setup</h2>

<p>You need to require 4 files to get everything setup properly:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">require</span> <span class="s1">&#39;omniauth-openid&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;openid&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;openid/store/filesystem&#39;</span>
</span><span class='line'><span class="nb">require</span> <span class="s1">&#39;gapps_openid&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>The first one is the omniauth extension for OpenID, the second one is
the main Ruby OpenID library (needed so we can set our SSL cert).
The third one allows us to store temporary data on disk instead of
keeping it in memory (optional).
And finally, the last one is Google&#8217;s magical gem to get their discovery
system working.</p>

<p>Because we are going to communicate via SSL, we want to make sure that
the OpenID library uses our certs to verify the SSL communications:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">OpenID</span><span class="o">.</span><span class="n">fetcher</span><span class="o">.</span><span class="n">ca_file</span> <span class="o">=</span> <span class="s2">&quot;/absolute/path/to/ssl_cacert.pem&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>(Obviously, you need to change the path to your own cert)</p>

<p>We are almost done with the setup, we just need two more things:</p>

<ul>
<li>make sure you are using a session.</li>
<li>setup OmniAuth</li>
</ul>


<p>I&#8217;m using Sinatra, so I&#8217;ll load the <code>Rack::Session</code> middleware before I
set Omniauth:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">use</span> <span class="no">Rack</span><span class="o">::</span><span class="no">Session</span><span class="o">::</span><span class="no">Cookie</span><span class="p">,</span> <span class="ss">:secret</span> <span class="o">=&gt;</span> <span class="s1">&#39;supers3cr3t&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>(Rails turns that option by default, so you don&#8217;t need to worry about
it)</p>

<p>Then I can finally setup OmniAuth:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">use</span> <span class="no">OmniAuth</span><span class="o">::</span><span class="no">Builder</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">provider</span> <span class="ss">:open_id</span><span class="p">,</span>  <span class="ss">:name</span> <span class="o">=&gt;</span> <span class="s1">&#39;admin&#39;</span><span class="p">,</span>
</span><span class='line'>                      <span class="ss">:identifier</span> <span class="o">=&gt;</span> <span class="s1">&#39;https://www.google.com/accounts/o8/site-xrds?hd=aimonetti.net&#39;</span><span class="p">,</span>
</span><span class='line'>                      <span class="ss">:store</span> <span class="o">=&gt;</span> <span class="no">OpenID</span><span class="o">::</span><span class="no">Store</span><span class="o">::</span><span class="no">Filesystem</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s1">&#39;/tmp&#39;</span><span class="p">)</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>There are two important things to notice. First, because I set the
provider&#8217;s name to be &#8216;admin&#8217;, the magical paths provided by OmiAuth
will use that name (<code>/auth/admin</code>). Secondly, and more importantly, notice how I added
the name of my Google Apps domain at the end of the identifier:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="s1">&#39;https://www.google.com/accounts/o8/site-xrds?hd=&#39;</span> <span class="o">+</span> <span class="n">your_domain_name</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Sinatra</h2>

<p>In Sinatra, your just need to define the routes OmniAuth would use (same
goes for Rails, just use the router for that).</p>

<p>By default, omniauth now offers you a <code>/auth/admin</code> endpoint that will
push the user through Google Apps authentication.
Once the authentication is over, the user will be redirected to the
following endpoint:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="c1"># Callback URL used when the authentication is done</span>
</span><span class='line'><span class="n">post</span> <span class="s1">&#39;/auth/admin/callback&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">auth_details</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">env</span><span class="o">[</span><span class="s1">&#39;omniauth.auth&#39;</span><span class="o">]</span>
</span><span class='line'>  <span class="n">session</span><span class="o">[</span><span class="ss">:email</span><span class="o">]</span> <span class="o">=</span> <span class="n">auth_details</span><span class="o">.</span><span class="n">info</span><span class="o">[</span><span class="s1">&#39;email&#39;</span><span class="o">]</span>
</span><span class='line'>  <span class="n">redirect</span> <span class="s1">&#39;/auth/admin/welcome&#39;</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can access the authentication details from <code>request.env['omniauth.auth']</code>
and redirect the user to another page, like the admin landing page for
instance.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">get</span> <span class="s1">&#39;/auth/admin/welcome&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="k">if</span> <span class="n">session</span><span class="o">[</span><span class="ss">:email</span><span class="o">]</span>
</span><span class='line'>    <span class="n">erb</span> <span class="ss">:welcome_boss</span>
</span><span class='line'>  <span class="k">else</span>
</span><span class='line'>    <span class="n">redirect</span> <span class="s1">&#39;/auth/admin&#39;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>On the landing page, you need to verify that the user is logged in, in
this case, during the previous step, we added the email of the user to
her session. We can therefore verify the presence of that information to
confirm the authentication status. If the user is authenticated, then we&#8217;ll render an ERB
template otherwise we&#8217;ll redirect her back to the login page.</p>

<p>We should also provide an endpoint in case the authentication failed:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">get</span> <span class="s1">&#39;/auth/failure&#39;</span> <span class="k">do</span>
</span><span class='line'>  <span class="n">params</span><span class="o">[</span><span class="ss">:message</span><span class="o">]</span>
</span><span class='line'>  <span class="c1"># do whatever you want here.</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Note that by default, in dev mode, Omniauth won&#8217;t redirect the user
there. To enable this behavior, use the following snippet (works with any rack app,
Rails, Sinatra or whatever):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">OmniAuth</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">on_failure</span> <span class="o">=</span> <span class="no">Proc</span><span class="o">.</span><span class="n">new</span> <span class="p">{</span> <span class="o">|</span><span class="n">env</span><span class="o">|</span>
</span><span class='line'>  <span class="no">OmniAuth</span><span class="o">::</span><span class="no">FailureEndpoint</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">env</span><span class="p">)</span><span class="o">.</span><span class="n">redirect_to_failure</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Conclusion</h2>

<p>Using Google Apps for authentication with OmniAuth is trivial as long
you know two things:</p>

<ul>
<li>the identifier url: <code>'https://www.google.com/accounts/o8/site-xrds?hd=' + your_domain_name</code></li>
<li>Google&#8217;s discovery service gem</li>
</ul>


<p>This blog post was written using <code>omniauth 1.1.1</code>, <code>omniauth-openid 1.0.1</code>, <code>rack-openid 1.3.1</code> and <code>ruby-openid-apps-discovery 1.2.0</code>. This might not apply to you if you come from the future :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Real life concurrency in Go]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/11/27/real-life-concurrency-in-go/"/>
    <updated>2012-11-27T10:08:00-08:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/11/27/real-life-concurrency-in-go</id>
    <content type="html"><![CDATA[<p>The structure of a programming language reflects the challenges and solutions the
designers decided to address. Each designer coming with his/her own background
decides to tackle some specific issues in a novel way and/or often
decides to borrow existing paradigms from other languages.
We can&#8217;t, then, fairly judge a language without understanding
what problem the language designer was trying to address.</p>

<p>Today we are going to look at <a href="http://golang.org/">Google&#8217;s Go language</a>.
Go approaches concurrency from an interesting view point. But instead of digging
into the history and reasoning which led to this approach, I&#8217;d like to
show you the language constructs by actually writing real life code.</p>

<h2>Fetching web resources concurrently</h2>

<p>The following example is taken from my recent presentation <a href="http://matt.aimonetti.net/posts/2012/11/02/rubyconf-2012-ruby-vs-the-world/">Ruby vs. the World</a>. I explored a few programming languages and
showed how they changed my Ruby.</p>

<p>To show how <a href="http://golang.org/">Go</a> addresses concurrency, I decided to build a
program which would concurrently fetch various web resources, wait for all of
them to be fetched, then process them all at once. In other
programming languages, we could have used <a href="http://en.wikipedia.org/wiki/Thread_(computing">threads</a>) and a <a href="http://en.wikipedia.org/wiki/Semaphore_(programming">semaphore</a>), <a href="http://en.wikipedia.org/wiki/Actor_model">actors</a> or
<a href="http://en.wikipedia.org/wiki/Callbacks">callbacks</a>. Go&#8217;s approach is <a href="http://en.wikipedia.org/wiki/Communicating_sequential_processes">slightly different</a>, let&#8217;s walk through the
code together.</p>

<p>The first part of our code gets us setup:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="k">package</span> <span class="n">main</span>
</span><span class='line'>
</span><span class='line'><span class="k">import</span> <span class="p">(</span>
</span><span class='line'>  <span class="s">&quot;fmt&quot;</span>
</span><span class='line'>  <span class="s">&quot;net/http&quot;</span>
</span><span class='line'>  <span class="s">&quot;time&quot;</span>
</span><span class='line'><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="k">var</span> <span class="n">urls</span> <span class="p">=</span> <span class="p">[]</span><span class="nb">string</span><span class="p">{</span>
</span><span class='line'>  <span class="s">&quot;http://www.rubyconf.com/&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s">&quot;http://golang.org/&quot;</span><span class="p">,</span>
</span><span class='line'>  <span class="s">&quot;http://matt.aimonetti.net/&quot;</span><span class="p">,</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The code above names our package then imports a few standard libraries that we are going to need. It then defines an array/slice of strings representing the urls we are going to fetch.</p>

<p>Next we define a type we will use a bit later:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="k">type</span> <span class="n">HttpResponse</span> <span class="k">struct</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">url</span>      <span class="nb">string</span>
</span><span class='line'>  <span class="n">response</span> <span class="p">*</span><span class="n">http</span><span class="p">.</span><span class="n">Response</span>
</span><span class='line'>  <span class="n">err</span>      <span class="n">error</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can think of a struct type as a simple representation of a class. Technically, we are defining a structure with some typed attributes. We will later on, create instances of this defined type.</p>

<p>Go implements OOP <a href="http://golang.org/doc/go_faq.html#Is_Go_an_object-oriented_language">slightly differently</a> than other languages.</p>

<blockquote><p>Methods in Go are more general than in C++, Java: they can be defined for any sort of data, even built-in types such as plain, “unboxed” integers. They are not restricted to structs (classes).</p></blockquote>

<p>We can therefore define methods/functions for any type of data,
including &#8220;any/all&#8221; types.
This approach to types is called <a href="http://en.wikipedia.org/wiki/Structural_type_system">structural typing</a>.</p>

<p>Here is the code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="k">func</span> <span class="n">asyncHttpGets</span><span class="p">(</span><span class="n">urls</span> <span class="p">[]</span><span class="nb">string</span><span class="p">)</span> <span class="p">[]*</span><span class="n">HttpResponse</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">ch</span> <span class="p">:=</span> <span class="nb">make</span><span class="p">(</span><span class="k">chan</span> <span class="p">*</span><span class="n">HttpResponse</span><span class="p">)</span>
</span><span class='line'>  <span class="n">responses</span> <span class="p">:=</span> <span class="p">[]*</span><span class="n">HttpResponse</span><span class="p">{}</span>
</span><span class='line'>  <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">url</span> <span class="p">:=</span> <span class="k">range</span> <span class="n">urls</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">go</span> <span class="k">func</span><span class="p">(</span><span class="n">url</span> <span class="nb">string</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="n">fmt</span><span class="p">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">&quot;Fetching %s \n&quot;</span><span class="p">,</span> <span class="n">url</span><span class="p">)</span>
</span><span class='line'>          <span class="n">resp</span><span class="p">,</span> <span class="n">err</span> <span class="p">:=</span> <span class="n">http</span><span class="p">.</span><span class="n">Get</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
</span><span class='line'>          <span class="n">ch</span> <span class="p">&lt;-</span> <span class="p">&amp;</span><span class="n">HttpResponse</span><span class="p">{</span><span class="n">url</span><span class="p">,</span> <span class="n">resp</span><span class="p">,</span> <span class="n">err</span><span class="p">}</span>
</span><span class='line'>      <span class="p">}(</span><span class="n">url</span><span class="p">)</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">for</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">select</span> <span class="p">{</span>
</span><span class='line'>      <span class="k">case</span> <span class="n">r</span> <span class="p">:=</span> <span class="p">&lt;-</span><span class="n">ch</span><span class="p">:</span>
</span><span class='line'>          <span class="n">fmt</span><span class="p">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">&quot;%s was fetched\n&quot;</span><span class="p">,</span> <span class="n">r</span><span class="p">.</span><span class="n">url</span><span class="p">)</span>
</span><span class='line'>          <span class="n">responses</span> <span class="p">=</span> <span class="n">append</span><span class="p">(</span><span class="n">responses</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span>
</span><span class='line'>          <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">responses</span><span class="p">)</span> <span class="p">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">urls</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>              <span class="k">return</span> <span class="n">responses</span>
</span><span class='line'>          <span class="p">}</span>
</span><span class='line'>      <span class="k">case</span> <span class="p">&lt;-</span><span class="n">time</span><span class="p">.</span><span class="n">After</span><span class="p">(</span><span class="mi">50</span> <span class="p">*</span> <span class="n">time</span><span class="p">.</span><span class="n">Millisecond</span><span class="p">):</span>
</span><span class='line'>          <span class="n">fmt</span><span class="p">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">&quot;.&quot;</span><span class="p">)</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>  <span class="k">return</span> <span class="n">responses</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is the meat of our application. And there is quite a lot of going
on in just a few lines. Assuming you aren&#8217;t familiar
with Go, I&#8217;ll walk though the code.</p>

<p>Let&#8217;s start with the signature:</p>

<ul>
<li>the function is named <code>asyncHttpGets</code></li>
<li>it takes an argument named<code>urls</code> which is an &#8220;array&#8221; of strings (I used quotes around the word
array because it&#8217;s technically what Go calls a slice)</li>
<li>it returns an &#8220;array&#8221; of <code>HttpResponse</code> pointers</li>
</ul>


<p>Then in the function body:</p>

<p>We start by creating an instance of a <code>channel</code> and assigning it to the
<code>ch</code> variable name. Think of a channel as a pipe like in unix.  We can write to and read from that channel.</p>

<p>In the next line we create an empty instance of a slice containing pointers to
<code>HttpResponse</code> objects.</p>

<p>Then, using the <code>for range</code> language construct, we iterate through our <code>urls</code>, storing the current value being used into the scoped variable <code>url</code>. The <code>url</code> is then available within the block/lambda/closure marked by the curly braces.</p>

<p>Now this is where the async construct comes in. Using the <code>go</code>
keyword, we define an anonymous function that takes a string argument representing a
url.</p>

<p>The function prints this string, then uses the <code>net/http</code>
library to fetch the web resource. We use the returned data to create an
instance of our <code>HttpResponse</code> type and send it to the channel.</p>

<p>This part gets a bit confusing because I reused the name <code>url</code>. We call this
anonymous function right away passing it the <code>url</code> variable set
by the loop.</p>

<p>You might wonder why we bother to create an anonymous function and
call it right away instead of just executing the code directly.
The <code>go</code> keyword executes the code that is passed in as a <em>goroutine</em> which is well explained <a href="http://golang.org/doc/effective_go.html#goroutines">here</a></p>

<blockquote><p>A goroutine is a function executing concurrently with other goroutines in the same address space. It is lightweight, costing little more than the allocation of stack space. And the stacks start small, so they are cheap, and grow by allocating (and freeing) heap storage as required.</p></blockquote>

<p>In other words, you start a <em>goroutine</em> and you let the &#8220;system&#8221; handle
how it wants to deal with the low level details. Technically, goroutines
might run in one or multiple threads, but you don&#8217;t need to know.
We trigger each http fetch in a separate goroutine
and then each response is pushed down the channel.</p>

<p>The second block of code begins with another <code>for</code> loop containing a switch/case statement.
The case statement checks if something is
in the channel. If there is something, we&#8230;</p>

<ul>
<li>allocate the data to the <code>r</code> variable</li>
<li>print the resource&#8217;s url</li>
<li>append the resource to the slice we created at the beginning of the function.</li>
</ul>


<p>If the length of the array is the same as the length of all urls we want to fetch, we are done
fetching all our resources and can return.
While still waiting for responses, we print a dot every 50ms.</p>

<p><strong>Update:</strong>
In the first version of this blog post I had used a &#8216;default&#8217; case
statement to print the dot and sleep for 50ms so the loop wouldn&#8217;t be
too tight and the concurrency effect was more obvious. But some
<a href="http://news.ycombinator.com/item?id=4837919">HN comments</a> pointed out that it wasn&#8217;t needed and I shouldn&#8217;t block.
For reference here is what I had before (don&#8217;t use this code):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="k">for</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">select</span> <span class="p">{</span>
</span><span class='line'>  <span class="k">case</span> <span class="n">r</span> <span class="p">:=</span> <span class="p">&lt;-</span><span class="n">ch</span><span class="p">:</span>
</span><span class='line'>      <span class="n">fmt</span><span class="p">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">&quot;%s was fetched\n&quot;</span><span class="p">,</span> <span class="n">r</span><span class="p">.</span><span class="n">url</span><span class="p">)</span>
</span><span class='line'>      <span class="n">responses</span> <span class="p">=</span> <span class="n">append</span><span class="p">(</span><span class="n">responses</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span>
</span><span class='line'>      <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">responses</span><span class="p">)</span> <span class="p">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">urls</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>          <span class="k">return</span> <span class="n">responses</span>
</span><span class='line'>      <span class="p">}</span>
</span><span class='line'>  <span class="k">default</span><span class="p">:</span>
</span><span class='line'>      <span class="n">fmt</span><span class="p">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">&quot;.&quot;</span><span class="p">)</span>
</span><span class='line'>      <span class="n">time</span><span class="p">.</span><span class="n">Sleep</span><span class="p">(</span><span class="mi">50</span> <span class="p">*</span> <span class="n">time</span><span class="p">.</span><span class="n">Millisecond</span><span class="p">)</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>
Thank you HackerNews.</p>

<p>With that code constructed, our <code>main</code> can make use of it like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='go'><span class='line'><span class="k">func</span> <span class="n">main</span><span class="p">()</span> <span class="p">{</span>
</span><span class='line'>  <span class="n">results</span> <span class="p">:=</span> <span class="n">asyncHttpGets</span><span class="p">(</span><span class="n">urls</span><span class="p">)</span>
</span><span class='line'>  <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">result</span> <span class="p">:=</span> <span class="k">range</span> <span class="n">results</span> <span class="p">{</span>
</span><span class='line'>      <span class="n">fmt</span><span class="p">.</span><span class="n">Printf</span><span class="p">(</span><span class="s">&quot;%s status: %s\n&quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">.</span><span class="n">url</span><span class="p">,</span>
</span><span class='line'>                 <span class="n">result</span><span class="p">.</span><span class="n">response</span><span class="p">.</span><span class="n">Status</span><span class="p">)</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Compiling and running the code look like this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='bash'><span class='line'><span class="nv">$ </span>go build concurrency_example.go <span class="o">&amp;&amp;</span> ./a.out
</span><span class='line'>.Fetching http://www.rubyconf.com/
</span><span class='line'>Fetching http://golang.org/
</span><span class='line'>Fetching http://matt.aimonetti.net/
</span><span class='line'>.....http://golang.org/ was fetched
</span><span class='line'>.......http://www.rubyconf.com/ was fetched
</span><span class='line'>.http://matt.aimonetti.net/ was fetched
</span><span class='line'>http://golang.org/ status: 200 OK
</span><span class='line'>http://www.rubyconf.com/ status: 200 OK
</span><span class='line'>http://matt.aimonetti.net/ status: 200 OK
</span></code></pre></td></tr></table></div></figure>


<p>As you can see from the print statements, the 3 urls are triggered in a
sequential way, but the responses come back in different orders due to different server latencies and response transfer time.</p>

<h2>Conclusion</h2>

<p>Go was designed to make concurrency easy for developers.
It is a very well documented language and you will find <a href="http://golang.org/doc/effective_go.html#concurrency">on this page
a lot of information about its concurrency philosophy</a> and details about each available constructs works.</p>

<p>I like that the language is very simple and the constructs
explicit. If you want to write concurrent code, Go pushes you to do it
in a specific style. That style is clear and comfortable for me. My code stays simple, I don&#8217;t go crazy with callbacks, and the
conventions make it simple for everyone else to understand my code.</p>

<p>Whether or not Go appeals to you stylistically, but clearly the designers
stayed close to the goal of developing to a 21st century C
with a special focus on concurrency with the unix approach.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Engineers suck at finding the right jobs]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/11/14/engineers-suck-at-finding-right-jobs/"/>
    <updated>2012-11-14T18:53:00-08:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/11/14/engineers-suck-at-finding-right-jobs</id>
    <content type="html"><![CDATA[<p>If you are currently a software engineer you need to realize two things:</p>

<ul>
<li><strong>Now is an awesome time to be a software engineer</strong> (probably the best time ever).</li>
<li><strong>Your job might not be well suited for you</strong>.</li>
</ul>


<p>I&#8217;ll show you why we are lucky bastards, why we aren&#8217;t so good at
picking the right jobs and some hints on how to solve this issue.</p>

<br>


<p>I remember a family friend telling me when I was a kid that computers
are going to be the future and that there will be a lot of jobs in this
field. I also remember that the idea of sitting all day, alone, in front of a
<a href="http://en.wikipedia.org/wiki/Minitel">minitel</a>-like computer scared the hell out of me.
But he was right and I now work from home, spending 12+ hours
in front of a monitor. I get emails and phone calls from many people
reaching out to me to help them find software engineers.</p>

<p>At least three things make &#8220;now&#8221; the best time to be a software
engineer:</p>

<ul>
<li><strong>demand</strong></li>
<li><strong>projects</strong></li>
<li><strong>prestige</strong></li>
</ul>


<h2>Good time</h2>

<p>There is a huge demand for engineers. There are many more job openings
than candidates. But this is much better than in late 90s/early 2000
when anyone who could write a line of HTML would get a job.
Now the projects people are building
are way more interesting and have a real potential to change lives.
Before, you had to work for a giant company to
have a chance to do that. But now, with internet and smart mobile
devices everywhere, almost any startup (fancy name for small company) can
have a huge impact &#8211; look at Twitter for instance.</p>

<p>Lastly, being a geek is cool. Movies, cartoons, TV Shows now have
geek heroes (granted they usually don&#8217;t represent real geeks, but hey
it&#8217;s better than nothing).</p>

<h2>The problem</h2>

<p><strong>Most software engineers</strong> I know, are really bad
at choosing the right job for themselves. They <strong>don&#8217;t design a career</strong>.
Engineers are good at solving technical problems in an objective way, but <strong>when it comes
to our jobs and future, we seem to struggle.</strong></p>

<h2>Why?</h2>

<p>I&#8217;m not an expert but I have a few guesses I&#8217;d like to share with you:</p>

<ul>
<li>We don&#8217;t know our real worth.</li>
<li>We don&#8217;t make long term plans.</li>
<li>We get paid well, so why bother changing?</li>
<li>Changing job feels like betrayal.</li>
<li>The system is broken.</li>
</ul>


<h2>Self worth</h2>

<p>Most modern companies need solid engineers to be relevant in the
short/medium term and they know it. <strong>Most engineers have no idea how
their talent and dedication converts into real business value</strong>.
Without that appreciation, they
can&#8217;t easily estimate how much they are worth. The
salary scale for software engineers is dramatically different from other jobs/industries.
To your business, you might be worth twice or three times the salary of someone like a teacher.
This is probably not fair because of the social value a teacher offers, but that&#8217;s the way the <a href="http://en.wikipedia.org/wiki/Law_of_demand">law of
demand</a> works in our society.
Knowing how much you are worth to a company and how much to ask is
critical to properly negotiate or renegotiate a contract.</p>

<p>I remember when I moved to America and was thankful to have a job.
I had no idea that at $45k/year with basically no health coverage and no
vacation, I was grossly underpaid and could have been paid twice that amount
down the street. (Note: the average software engineer salary in the US
is at <a href="http://www.indeed.com/salary/Software-Engineer.html">$89,000 according to indeed.com</a>).
The average salary for a high school teacher is <a href="http://www.indeed.com/salary?q1=high+school+teacher&amp;l1=">$47k/year</a>
so I wasn&#8217;t complaining. As a matter of fact, I didn&#8217;t leave this job because of the salary.
I truly believe that <strong>&#8220;Money doesn&#8217;t buy happiness&#8221;</strong> and <strong>it shouldn&#8217;t be
your primary reason to accept or leave a job</strong>. Money is nice and
often makes life easier. But the point is that you have to understand
how much you&#8217;re worth, so you can get paid and
focus on your work.</p>

<h2>Defining a vector</h2>

<p><a href="http://www.kitchensoap.com/about-me/">John Allspaw</a> wrote a great blog
post about <a href="http://www.kitchensoap.com/2012/10/25/on-being-a-senior-engineer/">what it means to be a senior engineer</a>.
I strongly recommend you read it. I often look back at it
and pick up a couple points I need to focus on myself.
John wrote a book which is a collection of essays and interviews
regarding tech/web ops.</p>

<p><a href="http://www.amazon.com/gp/product/1449377440/ref=as_li_ss_il?ie=UTF8&camp=1789&creative=390957&creativeASIN=1449377440&linkCode=as2&tag=merbist-20" style="text-align:center; display:block;"><img border="0" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=1449377440&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=merbist-20" ></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&l=as2&o=1&a=1449377440" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>

<p>Here is a very interesting quote:</p>

<blockquote><p>Not everyone can be senior. If, after five years, you are senior, are you at the peak of your game? After five more years will you not have accrued more invaluable experience? What then? “Super engineer”? Five more years? “Super-duper engineer.” I blame the youth of our discipline for this affliction. [&#8230;] Given the dynamics of our industry many elected to move on to managerial positions or risk an entrepreneurial run at things.”</p></blockquote>

<p>There are two very strong points in this quote:</p>

<ul>
<li>we don&#8217;t quite know what it means to be a senior engineer (and John&#8217;s post does a great
job explaining his take on that).</li>
<li>many of us end up in managerial positions or leading startups.</li>
</ul>


<p>I might be a bit radical &#8211; but the day I stop learning/improving
will be the day that I will quit, change jobs or careers. John&#8217;s post has great pointers
to help us improve our skills. But the question I&#8217;m trying to raise is:</p>

<p><strong>what do we want from our career?</strong></p>

<p>&#8220;Career&#8221; sounds like a dirty word to many of us. It has a corporate,
sleazy, back stabbing connotation. When I hear it, I picture a
cliché stock photography of a bunch of smiling people wearing 80&#8217;s suits.
In the context on this post, let&#8217;s take the Oxford English Dictionary
definition: &#8220;course or progress through life (or a distinct portion of life)&#8221;.
The word comes from from French via the Old Occitan word: &#8220;carriera&#8221; which means &#8220;street&#8221;.</p>

<p><strong>A better word for career might be &#8220;path&#8221;.</strong></p>

<p>I think we have a hard time knowing what kind of path we want to be on.
When faced with the question, a lot of us answer:
&#8220;solving problems&#8221;, &#8220;having fun&#8221;, &#8220;changing the world&#8221;.
All these answers sound good, but they aren&#8217;t paths, they are just attributes.</p>

<p>I have to admit that I&#8217;m still struggling with this question and
probably will for a while. I&#8217;m pretty good at defining short term goals but I
have a hard time seeing the long term path.
As a matter of fact, a little while back I was seated in front of <a href="http://www.chadfowler.com/">Chad
Fowler</a> in his office in Washington, DC.
Chad wrote a great book called <a href="http://www.amazon.com/gp/product/1934356344/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1934356344&amp;linkCode=as2&amp;tag=merbist-20">&#8220;The Passionate Programmer: Creating a Remarkable Career in Software Development&#8221;</a>.</p>

<p><a href="http://www.amazon.com/gp/product/1934356344/ref=as_li_ss_il?ie=UTF8&camp=1789&creative=390957&creativeASIN=1934356344&linkCode=as2&tag=merbist-20" style="text-align:center; display:block;"><img border="0" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=1934356344&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=merbist-20" ></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&l=as2&o=1&a=1934356344" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>

<p>I&#8217;ve read Chad&#8217;s book (which inspired me in many ways) and have known Chad
for years. The point of our meeting was to decide what I was going
to work on next. <strong>Unconsciously, I expected Chad to just tell me what I
should be doing. I trusted him to pick the right &#8220;path&#8221; for me.</strong>
But I was surprised when Chad told me: <em>&#8220;you&#8217;re the
kind of engineer who can do anything. You&#8217;re a generalist who can pick
a topic and become a specialist. So what do you want to do?&#8221;</em></p>

<p>I wasn&#8217;t sure how to take it, it sounded like a compliment but, at the
same time, <strong>the fact that Chad didn&#8217;t have a solution to my problem bothered me.</strong>
I remember thinking, wait, he&#8217;s the expert and he&#8217;s deflecting the
situation by asking me the question I came to ask him. Sure, the
compliment was nice but that wouldn&#8217;t solve anything. What does that
mean about me? If an expert can&#8217;t figure out what I should do, I might
be screwed.</p>

<p>Then on my way back home, I realized that <strong>it didn&#8217;t matter how well Chad knew me,
he couldn&#8217;t guess what I even didn&#8217;t know about myself.</strong></p>

<p><strong>My long term happiness depends on me finding a direction I want my
professional life to take.</strong>
In Chad&#8217;s book, there is a strong focus on finding a market,
understanding it, developing skills and marketing yourself.
However there was something I had missed.</p>

<blockquote><p>The goal-oriented, destination-focused thinking that you usually do
leads only from one goal to the next. It has no logical end. What most
of us fail to realize is that <em>the path</em> is the end.</p></blockquote>

<p>I&#8217;ve always known that the journey is more important than the destination in itself,
but what I had missed is that you still need to define a destination or
maybe more precisely a direction, a vector.
My problem is that my path was just a bunch of scattered dots. Hopping from one
to the other, I was hoping it was going to make a pretty drawing. The challenge is
when I got to a spot, I was stuck not knowing what to do next. I ended up picking another
short term goal/destination based on the opportunities available at that
time.</p>

<p>What I should do instead was to <strong>define a general direction and then
learn through the process.</strong> I believe this will help me enjoy my job more
than running after goals. It will allow me to see the world differently
and will help me make the right career choices when the time is right.
To be honest, I think that&#8217;s the only way I can build endurance and not
burn out in 5 years. That said, I&#8217;ll still have goals,
deadlines and the usual &#8211; but they won&#8217;t define my own personal progress.</p>

<h2>Why bother?</h2>

<p>Changing jobs is a pain. As an engineer I weigh the pros and cons and try
to logically pick the right choice &#8211; at least in theory. In practice I
avoid dealing with questions that might result in challenging
consequences.</p>

<p>Quiting a job is tough. You have to tell your current employer and your
colleagues that you are leaving them for something you think is better
for you. <strong>The nicer the people you work with, the harder it is. The better
you are paid, the harder it is.</strong> If you work with nice people and
you&#8217;re well paid, leaving is <em>really</em> hard (take note if you run a team).</p>

<p>In our profession, changing jobs isn&#8217;t usually seen as something
bad. Recruiters might pressure you to take a new, better job.
Beware impulsive changes though. Recruiters want their commissions so
they&#8217;ll do anything they can do make you switch. They&#8217;ll try to convince
you that the grass is greener on the other side. Maybe that&#8217;s only &#8220;bad&#8221; recruiters.</p>

<p>There are some recruiters who care about people and companies. Recruiters
who will help you find the right job for you. However, they
won&#8217;t be able to help you if you don&#8217;t know what direction you want to
go to.</p>

<p>I remember being stuck in a pretty terrible work environment, being
underpaid and the projects I was working on weren&#8217;t going anywhere.
A friend gave me a Seth Godin&#8217;s book called <a href="http://www.amazon.com/gp/product/1591841666/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1591841666&amp;linkCode=as2&amp;tag=merbist-20">&#8220;The Dip: A Little Book That Teaches You When to Quit (and When to Stick)&#8221;</a>.</p>

<p><a href="http://www.amazon.com/gp/product/1591841666/ref=as_li_ss_il?ie=UTF8&camp=1789&creative=390957&creativeASIN=1591841666&linkCode=as2&tag=merbist-20" style="text-align:center; display:block;"><img border="0" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&ASIN=1591841666&Format=_SL110_&ID=AsinImage&MarketPlace=US&ServiceVersion=20070822&WS=1&tag=merbist-20" ></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&l=as2&o=1&a=1591841666" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>

<p>I&#8217;m not a big fan of self-help/business books, but this book raised a very good and simple question:
how do the efforts compare to the returns?
Which situation are you in:</p>

<p><img src="http://matt.aimonetti.net/images/dip.jpg" alt="" /></p>

<p><img src="http://matt.aimonetti.net/images/cliff-dip.jpg" alt="" /></p>

<p>Think about it. <strong>Will the effort you put into your work pay off?</strong>
If you don&#8217;t think it will, then you should quit right away.</p>

<p>The logic is pretty simple but requires you to forecast. For that you need
some sort of metrics helping you to see if you are getting closer or
further from the direction you set for yourself.</p>

<h2>Trust</h2>

<p>Trust is the key element of any relationship.
In <a href="http://www.amazon.com/gp/product/B000UCUX0K/ref=as_li_ss_tl?ie=UTF8&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B000UCUX0K&amp;linkCode=as2&amp;tag=merbist-20">The Five Dysfunctions of a Team</a>, Patrick Lencioni explains that the base of management dysfunctions is absence of trust:</p>

<p><img src="http://matt.aimonetti.net/images/fivedysfunctions.gif" alt="" /></p>

<p>Turns out it&#8217;s the same thing for our careers. We need a team of people
to help us define a vision/direction and keep us honest and accountable.</p>

<p>Find people who you can trust to talk to about your professional goals,
your progress, failures and doubts. People who will be honest with you
and tell you things you might not want to hear. Find mentors and honest
people. These people don&#8217;t have to be working in the industry. They just
have to be able to listen and care.</p>

<p>People like that are extremely hard to find, but so are good executives.
<strong>I believe that having trustworthy friends (partners/family members..) who care is a big part of what
makes someone successful.</strong></p>

<h2>My small contribution</h2>

<p>As I explained earlier, I&#8217;m no expert and I also struggle with the issues I described.
However, I&#8217;d be glad to provide a bit of my free time to help you think
through these issues.</p>

<p><strong>A lot of you are doing a great job without the rest
of us noticing.</strong> If you don&#8217;t have a popular twitter account, blog, open
source projects, published books or given talks at conferences, it might
be hard to get yourself noticed or even know how much you&#8217;re worth.
Trust is a big deal and if you are considering moving on, you probably
don&#8217;t want your boss and your colleagues to know. You probably also
don&#8217;t know good recruiters you can trust. You might not even be sure
it&#8217;s worth investing too much time.</p>

<p>Most of you probably won&#8217;t consider it, but <strong>I&#8217;d like to offer my
help</strong>
if you&#8217;d like it. <strong>I promise full anonymity and no strings
attached</strong>. Just</p>

<ul>
<li><del>email me</del></li>
<li><del>tell me about yourself and what you do</del></li>
<li><del>what direction you&#8217;d like your career to take</del></li>
<li><del>ask any questions you might have </del></li>
</ul>


<p>After exchanging a few emails , <strong>I&#8217;ll try to make good use of my network</strong>
to find you a way to move in your desired direction.
Or, if you work for an interesting company with current openings, feel free to contact
me too.</p>

<p><strong> Update: My inbox is overflowing with emails and I already spent
literally days replying to as many people as possible. I&#8217;m sorry but at
this time I can&#8217;t reply to any new enquiries. I&#8217;ll write a follow up
blog post that covers what I learned from my interactions with so many
readers.
</strong></p>

<p>I have no idea how this will turn out. Maybe I&#8217;ll get a couple of emails,
zero, or way too many to handle &#8211; but it&#8217;s worth a try. I do have a full time job,
so please don&#8217;t expect me to reply to your emails within the hour.</p>

<p>I finally took the time to enable the comments in this blog. Feel free
to leave advice or feedback.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[RubyConf 2012 - Ruby vs. The World]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/11/02/rubyconf-2012-ruby-vs-the-world/"/>
    <updated>2012-11-02T20:47:00-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/11/02/rubyconf-2012-ruby-vs-the-world</id>
    <content type="html"><![CDATA[<p>During <a href="http://rubyconf.com/">RubyConf 2012</a> in Denver, Colorado Matt
Aimonetti gave a talk entitled <strong>Ruby Vs. The World</strong>.</p>

<h2>Description of the talk:</h2>

<p>Ruby is an awesome programming language, it&#8217;s so pleasing you probably haven&#8217;t seriously looked at other languages since you switched. The programming world is evolving fast, new languages are created daily, new trends are emerging. Let&#8217;s take some time to look at a few languages from a Ruby developer perspective.</p>

<p><img src="https://speakerd.s3.amazonaws.com/presentations/50941f6aeb710400020019ac/slide_19.jpg" alt="Matt Aimonetti talks Scala, Clojure, Go and Ruby" /></p>

<h2>Slides</h2>

<script async class="speakerdeck-embed" data-id="50941f6aeb710400020019ac" data-ratio="1.2994923857868" src="http://matt.aimonetti.net//speakerdeck.com/assets/embed.js"></script>


<p>The slides are available on [Matt&#8217;s SpeakerDeck page]https://speakerdeck.com/matt_aimonetti/ruby-vs-the-world) and can be <a href="https://speakerd.s3.amazonaws.com/presentations/50941f6aeb710400020019ac/aimonetti-ruby_vs_the_world_rubyconf_2012.pdf">downloaded here</a>.</p>

<h2>Video</h2>

<iframe width="640" height="360" src="http://www.youtube.com/embed/V_k3q37Tieg" frameborder="0" allowfullscreen></iframe>


<h2>Presentation website</h2>

<p>Matt&#8217;s presentation was filmed by <a href="http://confreaks.com">Confreaks</a> and posted <a href="http://confreaks.com/videos/1288-rubyconf2012-ruby-vs-the-world">here</a>.</p>

<p>For more RubyConf 2012 talks, go <a href="http://confreaks.com/events/rubyconf2012">there</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Aloha RubyConf - mmm..mruby or why yet another Ruby implementation]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/10/09/mmm-dot-mruby-or-why-yet-another-ruby-implementation/"/>
    <updated>2012-10-09T20:14:00-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/10/09/mmm-dot-mruby-or-why-yet-another-ruby-implementation</id>
    <content type="html"><![CDATA[<p>During <a href="http://aloharubyconf.com/">Aloha RubyConf 2012</a> in Honolulu, Hawaii Matt
Aimonetti gave a talk entitled <strong>mmm..mruby or why yet another Ruby implementation</strong>.</p>

<h2>Description of the talk:</h2>

<p>mruby is Matz’ new Ruby implementation, it’s not cooler than node.js, it doesn’t natively support Hypstermedia,
 it looks just like the good old Ruby. So why should we, as a community care?</p>

<p>Matt&#8217;s talk is divided in two parts, an introduction of mruby (embedded
Ruby) and and revisiting Ruby.</p>

<p><img src="https://speakerd.s3.amazonaws.com/presentations/50752712f8a4020002043005/slide_10.jpg?1350068026" alt="Matt Aimonetti talks about mruby" /></p>

<h2>Slides</h2>

<script async class="speakerdeck-embed" data-id="50752712f8a4020002043005" data-ratio="1.2994923857868" src="http://matt.aimonetti.net//speakerdeck.com/assets/embed.js"></script>


<p>The slides are available on [Matt&#8217;s SpeakerDeck page]https://speakerdeck.com/matt_aimonetti/mmmm-dot-mruby-everywhere-and-revisiting-ruby) and can be <a href="https://speakerd.s3.amazonaws.com/presentations/50752712f8a4020002043005/mmmmruby_aloha_rubyconf.pdf">downloaded here</a>.</p>

<h2>Video</h2>

<iframe width="640" height="360" src="http://www.youtube.com/embed/eZYRd86OTbk" frameborder="0" allowfullscreen></iframe>


<h2>Presentation website</h2>

<p>Matt&#8217;s presentation was filmed by <a href="http://confreaks.com">Confreaks</a> and posted <a href="http://confreaks.com/videos/1252-aloharuby2012-mmm-mruby-or-why-yet-another-ruby-implementation">here</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[PulsoConf 2012 - Tour of programming languages]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/10/05/pulsoconf-tour-of-programming-languages/"/>
    <updated>2012-10-05T15:09:00-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/10/05/pulsoconf-tour-of-programming-languages</id>
    <content type="html"><![CDATA[<p>During <a href="http://pulsoconf.co/">PulsoConf 2012</a> in Bogotá, Colombia Matt
Aimonetti gave a talk entitled <em>Tower of
Babel: a tour of programming languages</em>.</p>

<h2>Description of the talk:</h2>

<p>Programming languages affect the way one looks and solves problems. But
comparing programming languages isn&#8217;t as simple as drawing a table
comparing features.</p>

<p><img src="http://matt.aimonetti.net/images/matt_aimonetti_languages_table.jpg" alt="Matt Aimonetti compares programming languages" /></p>

<p>In his talk, Matt shows what he likes, dislikes, the philosophy and concrete example of how to use 7 programming
languages:</p>

<ul>
<li>Ruby</li>
<li>JavaScript</li>
<li>CoffeeScript</li>
<li>Objective-C</li>
<li>Clojure</li>
<li>Scala</li>
<li>Go</li>
</ul>


<h2>Slides</h2>

<script async class="speakerdeck-embed" data-id="50662c32244a9d000202ba53" data-ratio="1.299492385786802" src="http://matt.aimonetti.net//speakerdeck.com/assets/embed.js"></script>


<p>The slides are available on <a href="https://speakerdeck.com/u/matt_aimonetti/p/tower-of-babel-a-tour-of-programming-languages">Matt&#8217;s SpeakerDeck</a> and can be <a href="https://speakerd.s3.amazonaws.com/presentations/50662c32244a9d000202ba53/aimonetti_pulsoconf_2012.pdf">downloaded here</a>.</p>

<h2>Video</h2>

<p>TBD</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[What is Scala's pattern matching]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/09/20/what-is-scala-pattern-matching/"/>
    <updated>2012-09-20T21:18:00-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/09/20/what-is-scala-pattern-matching</id>
    <content type="html"><![CDATA[<p><a href="http://www.scala-lang.org/">Scala</a> is a very interesting programming
language. It has for goal to provide
both <a href="http://en.wikipedia.org/wiki/Object-oriented_programming">Object Oriented</a> and <a href="http://en.wikipedia.org/wiki/Functional_programming">Functional Programming</a> paradigms.
Now <a href="http://www.scala-lang.org/">Scala</a> isn&#8217;t the only recent programming language out there mixing the two paradigms.
<a href="http://www.ruby-lang.org/">Ruby</a>, <a href="http://en.wikipedia.org/wiki/JavaScript">JavaScript</a> and <a href="http://clojure.org/">Clojure</a> are other examples of popular
languages implementing both functional and OO programming patterns. Of
course, they each have a different take on the problem and that is what
is interesting.</p>

<p>Instead of arguing the pros and cons of OOP vs FP and how each of the
previously mentioned languages handle being OOP and FP, I&#8217;d like to introduce
a very powerful <a href="http://www.scala-lang.org/">Scala</a> idiom: <a href="http://en.wikipedia.org/wiki/Pattern_matching">pattern
matching</a>. Note that
pattern matching isn&#8217;t something Scala invented nor that it only exists in
Scala. Pattern matching can be achieved many different ways. However,
the majority of the popular languages don&#8217;t put this concept at the
center of their language. A few languages way before Scala rested
heavily on pattern matching such as <a href="http://www.erlang.org/doc/reference_manual/patterns.html">Erlang</a>,
<a href="http://www.haskell.org/haskellwiki/Haskell">Haskell</a> but that&#8217;s a different story.
How does Scala offers Pattern Matching, what is it and finally why is it
valuable?</p>

<h2>Scala pattern matching by examples</h2>

<p>As its name indicates, pattern matching is used to detect patterns.
Here is an example that covers a few interesting cases:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">def</span> <span class="n">listAnalysis</span><span class="o">(</span><span class="n">list</span><span class="k">:</span> <span class="kt">List</span><span class="o">[</span><span class="kt">Any</span><span class="o">])</span> <span class="k">=</span> <span class="n">list</span> <span class="k">match</span> <span class="o">{</span>
</span><span class='line'>   <span class="k">case</span> <span class="nc">Nil</span> <span class="k">=&gt;</span> <span class="s">&quot;empty&quot;</span>
</span><span class='line'>   <span class="k">case</span> <span class="-Symbol">&#39;a</span><span class="err">&#39;</span> <span class="o">::</span> <span class="n">tail</span> <span class="k">=&gt;</span> <span class="s">&quot;starting by &#39;a&#39;&quot;</span>
</span><span class='line'>   <span class="k">case</span> <span class="o">(</span><span class="n">head</span><span class="k">:</span><span class="kt">Int</span><span class="o">)</span> <span class="o">::</span> <span class="k">_</span> <span class="k">if</span> <span class="n">head</span> <span class="o">&gt;</span> <span class="mi">3</span> <span class="k">=&gt;</span> <span class="s">&quot;starting by an int greater than 3&quot;</span>
</span><span class='line'>   <span class="k">case</span> <span class="o">(</span><span class="n">head</span><span class="k">:</span><span class="kt">Int</span><span class="o">)</span> <span class="o">::</span> <span class="k">_</span> <span class="k">=&gt;</span> <span class="s">&quot;starting by an int&quot;</span>
</span><span class='line'>   <span class="k">case</span> <span class="k">_</span> <span class="k">=&gt;</span> <span class="s">&quot;whatever&quot;</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you&#8217;ve never seen any Scala that probably looks like gibberish to
you. Let me break it down:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">def</span> <span class="n">listAnalysis</span><span class="o">(</span><span class="n">list</span><span class="k">:</span> <span class="kt">List</span><span class="o">[</span><span class="kt">Any</span><span class="o">])</span> <span class="k">=</span> <span class="n">list</span> <span class="k">match</span> <span class="o">{}</span>
</span></code></pre></td></tr></table></div></figure>


<p>I define a new function called <code>listAnalysis</code> which takes an argument
named <code>list</code> which is of type <code>List</code> (this list could contain any kind
of elements).
The implementation of this function is a pattern match on the list
argument.
The body of this &#8216;pattern match&#8217; looks like a classical switch statement.
But it&#8217;s actually much more than a simple switch statement. Surely it
could be used like one, but as we will see, it can do much more.</p>

<p>Note that you can apply a pattern match against more than one object at
once as shown a bit later.</p>

<p>Let&#8217;s look at the statements inside the function.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">case</span> <span class="nc">Nil</span> <span class="k">=&gt;</span> <span class="s">&quot;empty&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>In this case we are checking that the list is empty or nil. If that&#8217;s
the case, the statement on the other side of the &#8220;fat arrow&#8221; is
executed. In this case, we return a string but we could have called
another function or so whatever.</p>

<p>Now the second statement is much more complex and much more powerful:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">case</span> <span class="-Symbol">&#39;a</span><span class="err">&#39;</span> <span class="o">::</span> <span class="n">tail</span> <span class="k">=&gt;</span> <span class="s">&quot;starting by &#39;a&#39;&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Remember that we are doing pattern matching against our list object.
What we are doing here is use the <code>::</code> operator (aka cons operator) to
extract the head and the rest of the list and then we match the head
against the <code>'a'</code> character.</p>

<p>This statement could have been written different ways:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">case</span> <span class="-Symbol">&#39;a</span><span class="err">&#39;</span> <span class="o">::</span> <span class="n">rest</span> <span class="k">=&gt;</span> <span class="s">&quot;starting by &#39;a&#39;&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>In that case we named the <code>tail</code> of the list <code>rest</code>, but really we don&#8217;t
care how it&#8217;s called or its value, so the sensitive thing to do is to
rewrite that statement like that:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">case</span> <span class="-Symbol">&#39;a</span><span class="err">&#39;</span> <span class="o">::</span> <span class="k">_</span> <span class="k">=&gt;</span> <span class="s">&quot;starting by &#39;a&#39;&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This basically says we are looking for a list that starts by <code>'a'</code> (and we
don&#8217;t care about the rest).</p>

<p>Another statement:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">case</span> <span class="o">(</span><span class="n">head</span><span class="k">:</span><span class="kt">Int</span><span class="o">)</span> <span class="o">::</span> <span class="k">_</span> <span class="k">=&gt;</span> <span class="s">&quot;starting by an int&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>In this case we type match the first element of the list and check that
we have an integer. Note that using the cons operator in the match cases
doesn&#8217;t seem to affect performance. It would seem that at compilation,
the statement are rewritten to avoid creating uneeded objects (List also implements structural sharing of the tail list).
I&#8217;m not a Scala expert so someone with more experience might be able to
confirm/clarify.</p>

<p>Now let&#8217;s look at a variant of this statement:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">case</span> <span class="o">(</span><span class="n">head</span><span class="k">:</span><span class="kt">Int</span><span class="o">)</span> <span class="o">::</span> <span class="k">_</span> <span class="k">if</span> <span class="n">head</span> <span class="o">&gt;</span> <span class="mi">3</span> <span class="k">=&gt;</span> <span class="s">&quot;starting by an int greater than 3&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is the same statement as above, but we are adding an extra
condition after the match. This is quite useful when simple matching
doesn&#8217;t cut it.</p>

<p>Finally we have a fallback statement:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">case</span> <span class="k">_</span> <span class="k">=&gt;</span> <span class="s">&quot;whatever&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>For more information about the cons operator, <a href="http://www.scala-lang.org/node/112">read about the extractor objects</a> and what they can do.</p>

<p>Here is the result of calling our function with different lists:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="n">listAnalysis</span><span class="o">(</span><span class="nc">List</span><span class="o">())</span>                             <span class="c1">//&gt; java.lang.String = empty</span>
</span><span class='line'><span class="n">listAnalysis</span><span class="o">(</span><span class="s">&quot;This is a test&quot;</span><span class="o">.</span><span class="n">toList</span><span class="o">)</span>            <span class="c1">//&gt; java.lang.String = whatever</span>
</span><span class='line'><span class="n">listAnalysis</span><span class="o">(</span><span class="s">&quot;abcde&quot;</span><span class="o">.</span><span class="n">toList</span><span class="o">)</span>                     <span class="c1">//&gt; java.lang.String = starting by &#39;a&#39;</span>
</span><span class='line'><span class="n">listAnalysis</span><span class="o">(</span><span class="nc">List</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span><span class="mi">2</span><span class="o">,</span><span class="mi">3</span><span class="o">))</span>                        <span class="c1">//&gt; java.lang.String = starting by an int</span>
</span><span class='line'><span class="n">listAnalysis</span><span class="o">(</span><span class="nc">List</span><span class="o">(</span><span class="mi">42</span><span class="o">,</span><span class="mi">24</span><span class="o">,</span><span class="mi">36</span><span class="o">))</span>                     <span class="c1">//&gt; java.lang.String = starting by an int greater than 3</span>
</span><span class='line'><span class="n">listAnalysis</span><span class="o">(</span><span class="s">&quot;a&quot;</span><span class="o">.</span><span class="n">toList</span><span class="o">)</span>                         <span class="c1">//&gt; java.lang.String = starting by &#39;a&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Here is another example using 2 items for the match:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='scala'><span class='line'><span class="k">def</span> <span class="n">doubleMatch</span><span class="o">(</span><span class="n">foo</span><span class="k">:</span> <span class="kt">Any</span><span class="o">,</span> <span class="n">bar</span><span class="k">:</span> <span class="kt">Any</span><span class="o">)</span> <span class="k">=</span> <span class="o">(</span><span class="n">foo</span><span class="o">,</span> <span class="n">bar</span><span class="o">)</span> <span class="k">match</span> <span class="o">{</span>
</span><span class='line'>  <span class="k">case</span> <span class="o">(</span><span class="-Symbol">&#39;a</span><span class="err">&#39;</span><span class="o">,</span> <span class="-Symbol">&#39;b</span><span class="err">&#39;</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="s">&quot;a and b&quot;</span>
</span><span class='line'>  <span class="k">case</span> <span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="-Symbol">&#39;b</span><span class="err">&#39;</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="s">&quot;1 and b&quot;</span>
</span><span class='line'>  <span class="k">case</span> <span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="k">_</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="s">&quot;1 and &quot;</span><span class="o">+</span> <span class="n">bar</span>
</span><span class='line'>  <span class="k">case</span> <span class="o">(</span><span class="n">a</span><span class="k">:</span><span class="kt">Float</span><span class="o">,</span> <span class="k">_</span><span class="o">)</span> <span class="k">=&gt;</span> <span class="s">&quot;foo float&quot;</span>
</span><span class='line'>  <span class="k">case</span> <span class="k">_</span> <span class="k">=&gt;</span> <span class="s">&quot;unknown case&quot;</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="n">doubleMatch</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="s">&quot;test&quot;</span><span class="o">)</span>                           <span class="c1">//&gt; java.lang.String = 1 and test</span>
</span><span class='line'><span class="n">doubleMatch</span><span class="o">(</span><span class="mi">1</span><span class="o">,</span> <span class="-Symbol">&#39;b</span><span class="err">&#39;</span><span class="o">)</span>                              <span class="c1">//&gt; java.lang.String = 1 and b</span>
</span><span class='line'><span class="n">doubleMatch</span><span class="o">(</span><span class="mi">42</span><span class="o">,</span> <span class="nc">Nil</span><span class="o">)</span>                             <span class="c1">//&gt; java.lang.String = unknown case</span>
</span><span class='line'><span class="n">doubleMatch</span><span class="o">(</span><span class="-Symbol">&#39;a</span><span class="err">&#39;</span><span class="o">,</span> <span class="-Symbol">&#39;b</span><span class="err">&#39;</span><span class="o">)</span>                            <span class="c1">//&gt; java.lang.String = a and b</span>
</span><span class='line'><span class="n">doubleMatch</span><span class="o">(</span><span class="mf">4.2f</span><span class="o">,</span> <span class="mi">42</span><span class="o">)</span>                            <span class="c1">//&gt; java.lang.String = foo float</span>
</span></code></pre></td></tr></table></div></figure>


<h1>Why is pattern matching valuable?</h1>

<p>In short, pattern matching allows the developer to deconstruct a structure to find specific
elements, in other words the pattern, needed to then constuct an
object/structure or trigger a function.</p>

<p>It&#8217;s the opposite process of calling a method on an object. Here we
start from a structure (instead of the instance of an object), this structure is just a basic struct and
based on a found pattern, we then trigger a function (with access to the data if we need it).
When you have a stable and known data structure, it&#8217;s often very interesting to
use the pattern matching approach because you can easily expand the
operations you can execute. However, if your operations are stable but the data changes,
then the Object Oriented approach seems more adequate.</p>

<p>Besides that, pattern matching will often make your code clearer than
using if/else statements. Especially in a language like Scala where you
can define pattern matching function within a function and you can also
pass pattern matching functions around. Like eveything else, it needs to be used with caution so the intend of
the code is still understandable. That said it&#8217;s a great tool to have handy and I&#8217;ve
had a lot of fun rewriting my newbie Scala code using a more idiomatic
approach based on pattern matching.</p>

<p>I hope you enjoyed this quick introduction. You can read more about pattern matching in Scala in the following articles:
 <em>(note: <a href="http://ikaisays.com">Ikai</a>&#8217;s post on how he uses regexps with pattern matching is a fun read.)</em></p>

<ul>
<li><a href="http://www.scala-lang.org/node/120">http://www.scala-lang.org/node/120</a></li>
<li><a href="http://pragprog.com/magazines/2012-03/scala-for-the-intrigued">http://pragprog.com/magazines/2012-03/scala-for-the-intrigued</a></li>
<li><a href="http://kerflyn.wordpress.com/2011/02/14/playing-with-scalas-pattern-matching/">http://kerflyn.wordpress.com/2011/02/14/playing-with-scalas-pattern-matching/</a></li>
<li><a href="http://ikaisays.com/2009/04/04/using-pattern-matching-with-regular-expressions-in-scala/">http://ikaisays.com/2009/04/04/using-pattern-matching-with-regular-expressions-in-scala/</a></li>
<li><a href="http://www.artima.com/scalazine/articles/pattern_matching.html">http://www.artima.com/scalazine/articles/pattern_matching.html</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ruby constructs: class, module and mixin]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/07/30/ruby-class-module-mixins/"/>
    <updated>2012-07-30T14:46:00-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/07/30/ruby-class-module-mixins</id>
    <content type="html"><![CDATA[<p>When you first get started with the Ruby programming and you come from a different
language, the only tricky piece is often Ruby&#8217;s approach to block/closure/anonymous functions.
Sure the metaprogramming seems a bit odd, but you don&#8217;t have to use it.
That&#8217;s why a lot of developers think that Ruby is a simple language.
Turns out that when you dig a bit further, you realize that Ruby is
actually quite a complex language. Ask any developer who worked on a
Ruby implementation, they&#8217;ll all tell you the same thing: Ruby is full of
small little things that makes it complicated.</p>

<p>An example of something that might seem simple is inheritance. Ruby,
unlike C++, doesn&#8217;t support multiple inheritance. What that means is
that a Ruby class can only have 1 parent class (superclass). However
multiple inheritance can be achieved via modules used as a mixins.
That&#8217;s a very common pattern, people put some code in a module and then
mix it in/include it in a bunch of classes.
The problem I see though, is that people abuse these concepts and don&#8217;t
respect the difference between a class, a module and a module used a
mixin.</p>

<h2>The Class</h2>

<p>Object Oriented Programming 101:</p>

<blockquote><p>&#8220;In object-oriented programming, a class is a construct that is used to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable its instances to have state and behavior.&#8221;</p></blockquote>

<p>So if you create a class and you don&#8217;t create instances, you are using
the wrong construct. Here is an example of what I often see:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Settings</span>
</span><span class='line'>
</span><span class='line'>  <span class="vi">@settings</span> <span class="o">=</span> <span class="p">{}</span>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">all</span>
</span><span class='line'>    <span class="vi">@settings</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">[]</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
</span><span class='line'>    <span class="n">all</span><span class="o">[</span><span class="n">key</span><span class="o">]</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">[]=</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</span><span class='line'>    <span class="n">all</span><span class="o">[</span><span class="n">key</span><span class="o">]</span> <span class="o">=</span> <span class="n">value</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which can be used as such:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="no">Settings</span><span class="o">[</span><span class="ss">:secret</span><span class="o">]</span> <span class="o">=</span> <span class="mi">42</span> <span class="o">*</span> <span class="no">Math</span><span class="o">::</span><span class="no">PI</span> <span class="o">*</span> <span class="no">Time</span><span class="o">.</span><span class="n">now</span><span class="o">.</span><span class="n">to_f</span>
</span><span class='line'><span class="nb">p</span> <span class="no">Settings</span><span class="o">[</span><span class="ss">:secret</span><span class="o">]</span>
</span><span class='line'><span class="c1"># =&gt; 177243152913.2707</span>
</span></code></pre></td></tr></table></div></figure>


<p><em>(granted this isn&#8217;t a great example since we could have used a subclass
of <code>Hash</code> but just bear with me)</em></p>

<p>Actually, the developer who wrote the code above would probably also use
some Ruby magic like <code>method_missing</code> to provide a more laxed API and allow for &#8220;nicer&#8221; getters
such as <code>Settings.secret</code> and <code>Settings['secret']</code>. I have my
own thoughts on the topic but it&#8217;s
an entirely different subject.</p>

<p>Note also that the way class level methods are defined
can also be different depending on who wrote the code, you might see the
following variations (and other more esoteric ones):</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Settings</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nc">Settings</span><span class="o">.</span><span class="nf">all</span><span class="p">;</span> <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># or</span>
</span><span class='line'>  <span class="k">class</span> <span class="o">&lt;&lt;</span> <span class="nb">self</span>
</span><span class='line'>    <span class="k">def</span> <span class="nf">all</span><span class="p">;</span> <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The <code>Settings</code> code above works, the code is simple, yet I will argue one thing: <strong>it&#8217;s
an abuse of the class construct</strong>. We&#8217;re breaking the #1 rule of classes:
<em>&#8220;create instances of self&#8221;</em>.</p>

<p><strong>It&#8217;s easy, whenever you don&#8217;t create instances of a class,
please don&#8217;t use a class.</strong></p>

<p>That&#8217;s also true for slightly different examples such as:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">API</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">fetch</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span>
</span><span class='line'>    <span class="no">HTTP</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;http://matt.aimonetti.net/article/:id&#39;</span><span class="p">,</span> <span class="ss">:id</span> <span class="o">=&gt;</span> <span class="nb">id</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">resource</span> <span class="o">=</span> <span class="no">API</span><span class="o">.</span><span class="n">new</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>There is no need whatsoever to create an instance of <code>API</code>, using a class is
picking the wrong construct. Also, I don&#8217;t care if you use the <code>Singleton</code>
module to only allow 1 instance of the class, you still shouldn&#8217;t use a
class in the above example.</p>

<h2>The Module</h2>

<p>In Ruby&#8217;s object hierarchy, the Class object actually inherits from the
Module object.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">ruby</span> <span class="o">-</span><span class="n">v</span> <span class="o">-</span><span class="n">e</span> <span class="s2">&quot;p Class.ancestors&quot;</span>
</span><span class='line'><span class="n">ruby</span> <span class="mi">1</span><span class="o">.</span><span class="mi">9</span><span class="o">.</span><span class="mi">3</span><span class="n">p194</span> <span class="p">(</span><span class="mi">2012</span><span class="o">-</span><span class="mo">04</span><span class="o">-</span><span class="mi">20</span> <span class="n">revision</span> <span class="mi">35410</span><span class="p">)</span> <span class="o">[</span><span class="n">x86_64</span><span class="o">-</span><span class="n">darwin11</span><span class="o">.</span><span class="mi">3</span><span class="o">.</span><span class="mi">0</span><span class="o">]</span>
</span><span class='line'><span class="o">[</span><span class="no">Class</span><span class="p">,</span> <span class="no">Module</span><span class="p">,</span> <span class="no">Object</span><span class="p">,</span> <span class="no">Kernel</span><span class="p">,</span> <span class="no">BasicObject</span><span class="o">]</span>
</span></code></pre></td></tr></table></div></figure>


<p>As per Ruby&#8217;s source code defintion:</p>

<blockquote><p>&#8220;A Module is a collection of methods and constants.&#8221;</p></blockquote>

<p>I like to think of modules as namespaced methods and constants. Whenever
you want code that logically belongs together but that
won&#8217;t require that you create instances of a &#8216;model&#8217;, then a module is
the right construct to use.</p>

<p>As a matter of fact, the two examples above are great cases where a
module should have been used.</p>

<p>The confusing bit is that modules can have module level methods but also
instance level methods. Here is an example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">API</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">fetch</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span>
</span><span class='line'>    <span class="no">HTTP</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;http://matt.aimonetti.net/article/:id&#39;</span><span class="p">,</span> <span class="ss">:id</span> <span class="o">=&gt;</span> <span class="nb">id</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>This is a module level function, it could also be written like that:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">API</span>
</span><span class='line'>
</span><span class='line'>  <span class="kp">module_function</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">fetch</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span>
</span><span class='line'>    <span class="no">HTTP</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;http://matt.aimonetti.net/article/:id&#39;</span><span class="p">,</span> <span class="ss">:id</span> <span class="o">=&gt;</span> <span class="nb">id</span><span class="p">)</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>And be used like that:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">resource</span> <span class="o">=</span> <span class="no">API</span><span class="o">.</span><span class="n">fetch</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Until now, it makes sense. The weird thing is that even though, modules
unlike classes aren&#8217;t meant to create instances, we have the possibility
to define module instance methods.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">module</span> <span class="nn">Settings</span>
</span><span class='line'>
</span><span class='line'>  <span class="no">DATA</span> <span class="o">=</span> <span class="p">{</span><span class="n">repo</span><span class="p">:</span> <span class="s1">&#39;http://github.com/mattetti&#39;</span><span class="p">}</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">repository</span>
</span><span class='line'>    <span class="no">DATA</span><span class="o">[</span><span class="ss">:repo</span><span class="o">]</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">secret_key</span>
</span><span class='line'>    <span class="no">DATA</span><span class="o">[</span><span class="ss">:key</span><span class="o">]</span> <span class="o">||=</span> <span class="mi">42</span><span class="o">*</span><span class="no">Math</span><span class="o">::</span><span class="no">PI</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>Great, but we can&#8217;t actually use these methods since they are instance
methods and we don&#8217;t create instances of modules. Well, that isn&#8217;t quite
true, there is a way to access them and that&#8217;s by using a module as a
mixin. What that means is that we inject/copy the module code inside a
class or an(other) object. Example in code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="no">Object</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="n">a</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="no">Settings</span><span class="p">)</span>
</span><span class='line'><span class="n">a</span><span class="o">.</span><span class="n">repository</span>
</span><span class='line'><span class="c1"># =&gt; &quot;http://github.com/mattetti&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Or we can add the code to a class so the instances of this class can
access our module instance methods:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Foo</span>
</span><span class='line'>  <span class="kp">include</span> <span class="no">Settings</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="no">Foo</span><span class="o">.</span><span class="n">new</span><span class="o">.</span><span class="n">repository</span>
</span><span class='line'><span class="c1"># =&gt; &quot;http://github.com/mattetti&quot; </span>
</span></code></pre></td></tr></table></div></figure>


<h2>The mixin modules</h2>

<p>There a plenty of resources online about the many ways to use mixins in Ruby to achieve multiple inheritance and do cool stuff.
But the point of this article is to try to demonstrate that mixins
shouldn&#8217;t be abused.</p>

<p>My problem with the above example is that by mixing in the <code>Settings</code>
module inside our <code>Foo</code> class, we created an uneeded, confusing extra
level of abstraction. Instances of <code>Foo</code> now have access to two
methods/objects: <code>repository</code> and <code>secret_key</code>. These methods or the
objects they refer to don&#8217;t belong to the <code>Foo</code> class, but it seems
convenient to not have to type <code>Settings.repository</code> so we mixed things
in. Plus, a lot of Ruby developers seem to dislike adding class/module
level methods so they feel that this approach &#8216;feels better&#8217;.</p>

<p>Here is the thing, the convenience of typing a few less characters isn&#8217;t
worth it. Next time you or someone else will look at an instance of the
<code>Foo</code> class calling <code>repository</code>, finding where it is defined is going
to be a pain. That&#8217;s especially true if you have many mixins in your
class. <code>Settings</code> will also probably grow and you will end up with a
bunch of methods that have nothing to do with your class instances.
In this case, I will call the use of a mixin, an abuse of construct.
Sure, Ruby allows you to do it, but that doesn&#8217;t mean it&#8217;s the right
thing to do. In Ruby, unlike in Python, there are 101 ways to do a
simple thing. It doesn&#8217;t mean that the 101 ways are good, it just means
that Matz wasn&#8217;t sure how people would use his programming language and
chose to give us more freedom to messup/doing it our own way.</p>

<h3>When to use mixins?</h3>

<p>I have my own rule: use mixins whenever you need to <em>share behaviors</em> between
different classes.</p>

<p>In the above example, we weren&#8217;t sharing behaviors, we were sharing
objects, there was no need to actually use a mixin.</p>

<p>That said, rules aren&#8217;t rules without exceptions. A good example of this
exception would be the <code>Math</code> module from the standard library.
This module offers trigonometric and transcendental functions. You might
think that this module would be designed to be a mixin so you can get
<code>log</code>, <code>cos</code>, <code>exp</code> and friends available in your math related classes.
It turns out, all Math&#8217;s methods are defined a module functions meaning
that they are meant to be called from the <code>Math</code> module directly.</p>

<p>However, Ruby allows you to mixin module functions, but these functions
become private. If you do include the module inside your class, your instance methods
will be able to call <code>hypot(x,y)</code> directly, but these methods won&#8217;t be
available from the outside (<code>Foo.new.log(42)</code> would raise an
exception).</p>

<p>To conclude with mixins: mixins are great but don&#8217;t abuse them or you
will endup with so much abstraction that your coworkers will secretely
call you <a href="http://en.wikipedia.org/wiki/Wassily_Kandinsky">Kandinsky</a>.
Stick to simple mixins allowing you to share behaviors between at least
a couple classes. See <code>DataMapper</code> for a great way to use mixins.</p>

<h2>Modules: your secret functional programming weapon</h2>

<p>I have to say that I do like functional programming. The idea of having
functions not mutating the states of things around them pleases me. It
just seems clean, you feed data to a function and you get another piece
of data. No states were changed, maybe some temporary variables were
allocated to process the data, but the only thing that matters is the
input and the output. Easy to grasp, easy to follow, no magical states
being changed by some code fairies.</p>

<p>The good news is that Ruby allows us to write code like that. And this
is where modules are great. Very much like the <code>Math</code> module we
discussed above, there are many cases where you want to have a bunch of
functions that process an input and provide an output without keeping
any states. A good example of such a module would be a param
verification filter. The filter takes an input, takes some rules and
verifies that the input matches the rules.
Surely, we could create an instance for each verification, this would
allow to keep states in our class and do the usual OOP things. But we
could also simply use a module with a bunch of module (level) functions
that would pass to each other the input they need to not need to keep
states. The end result will be faster, nicer on the GC and easy to
follow.</p>

<p>Mixing OOP and functional programming isn&#8217;t new, ask <a href="http://www.scala-lang.org/">Scala</a> developers!
If done right, by adopting this approach we can simply our code base,
make it faster, easier to maintain and not losing the chance to also use
OOP paradigms when needed.</p>

<h2>Compromise</h2>

<p>As shown earlier, modules and classes have pros and cons. Classes are
however much more natural to use for Object Oriented Programming. A
compromise about the <code>Settings</code> examples was suggested by Evan Phoenix.
The solution is elegant and simple. Use a class and an instance.
Here is an implementation based on his suggestion:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">AppSettings</span> <span class="o">&lt;</span> <span class="no">Hash</span>
</span><span class='line'>
</span><span class='line'>  <span class="k">def</span> <span class="nf">custom_method</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="no">SETTINGS</span> <span class="o">=</span> <span class="no">AppSettings</span><span class="o">.</span><span class="n">new</span>
</span></code></pre></td></tr></table></div></figure>


<p>The point here is not about the class implementation but that fact that we use
a class and associate an instance of this class to a constant so it can
be shared all over the place. Wow, a constant, this is so nasty you
might think. Classes are constants too and so are modules, here we just
allocate an instance of a class to a constant. Surpisingly simple and efficient.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Ruby: the differences between dup &amp; clone]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/07/28/ruby-the-differences-between-dup-and-clone/"/>
    <updated>2012-07-28T12:20:00-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/07/28/ruby-the-differences-between-dup-and-clone</id>
    <content type="html"><![CDATA[<p>Have you ever wondered what the differences are between <strong>#dup</strong> and <strong>#clone</strong> in Ruby?</p>

<p>They both create a shallow copy of an object (meaning that they don&#8217;t copy the objects that might be referenced within the copied object). However, <strong>#clone</strong> does two things that <strong>#dup</strong> doesn&#8217;t:</p>

<ul>
<li>copy the singleton class of the copied object</li>
<li>maintain the frozen status of the copied object</li>
</ul>


<p>Examples of the singleton methods not being copied.</p>

<p>dup:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="no">Object</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="k">def</span> <span class="nc">a</span><span class="o">.</span><span class="nf">foo</span><span class="p">;</span> <span class="ss">:foo</span> <span class="k">end</span>
</span><span class='line'><span class="nb">p</span> <span class="n">a</span><span class="o">.</span><span class="n">foo</span>
</span><span class='line'><span class="c1"># =&gt; :foo</span>
</span><span class='line'><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">dup</span>
</span><span class='line'><span class="nb">p</span> <span class="n">b</span><span class="o">.</span><span class="n">foo</span>
</span><span class='line'><span class="c1"># =&gt; undefined method `foo&#39; for #&lt;Object:0x007f8bc395ff00&gt; (NoMethodError)</span>
</span></code></pre></td></tr></table></div></figure>


<p>vs clone:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="no">Object</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="k">def</span> <span class="nc">a</span><span class="o">.</span><span class="nf">foo</span><span class="p">;</span> <span class="ss">:foo</span> <span class="k">end</span>
</span><span class='line'><span class="nb">p</span> <span class="n">a</span><span class="o">.</span><span class="n">foo</span>
</span><span class='line'><span class="c1"># =&gt; :foo</span>
</span><span class='line'><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">clone</span>
</span><span class='line'><span class="nb">p</span> <span class="n">b</span><span class="o">.</span><span class="n">foo</span>
</span><span class='line'><span class="c1"># =&gt; :foo</span>
</span></code></pre></td></tr></table></div></figure>


<p>Frozen state:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="no">Object</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="n">a</span><span class="o">.</span><span class="n">freeze</span>
</span><span class='line'><span class="nb">p</span> <span class="n">a</span><span class="o">.</span><span class="n">frozen?</span>
</span><span class='line'><span class="c1"># =&gt; true</span>
</span><span class='line'><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">dup</span>
</span><span class='line'><span class="nb">p</span> <span class="n">b</span><span class="o">.</span><span class="n">frozen?</span>
</span><span class='line'><span class="c1"># =&gt; false</span>
</span><span class='line'><span class="n">c</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">clone</span>
</span><span class='line'><span class="nb">p</span> <span class="n">c</span><span class="o">.</span><span class="n">frozen?</span>
</span><span class='line'><span class="c1"># =&gt; true</span>
</span></code></pre></td></tr></table></div></figure>


<p>Looking at the <a href="https://github.com/rubinius/rubinius/blob/master/kernel/alpha.rb#L230">Rubinius source code</a> makes the difference extremely obvious.</p>

<p>Because of the extra steps, <strong>clone</strong> is a bit slower than <strong>dup</strong> but that&#8217;s probably <strong>not</strong> what will make your app too slow.</p>

<p>Just a quick note about shallow copies (true for <strong>clone</strong> and <strong>dupe</strong>). Notice how the array referenced by the bar attribute doesn&#8217;t get copied but shared between the original and the copied instances:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="k">class</span> <span class="nc">Foo</span>
</span><span class='line'>  <span class="kp">attr_accessor</span> <span class="ss">:bar</span>
</span><span class='line'>  <span class="k">def</span> <span class="nf">initialize</span>
</span><span class='line'>    <span class="nb">self</span><span class="o">.</span><span class="n">bar</span> <span class="o">=</span> <span class="o">[</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="o">]</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span><span class='line'>
</span><span class='line'><span class="n">a</span> <span class="o">=</span> <span class="no">Foo</span><span class="o">.</span><span class="n">new</span>
</span><span class='line'><span class="n">b</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">clone</span>
</span><span class='line'><span class="nb">p</span> <span class="n">a</span><span class="o">.</span><span class="n">bar</span>
</span><span class='line'><span class="c1"># =&gt; [1, 2, 3]</span>
</span><span class='line'><span class="nb">p</span> <span class="n">b</span><span class="o">.</span><span class="n">bar</span>
</span><span class='line'><span class="c1"># =&gt; [1, 2, 3]</span>
</span><span class='line'><span class="n">a</span><span class="o">.</span><span class="n">bar</span><span class="o">.</span><span class="n">clear</span> <span class="c1"># clearing the array #bar points to</span>
</span><span class='line'><span class="nb">p</span> <span class="n">a</span><span class="o">.</span><span class="n">bar</span>
</span><span class='line'><span class="c1"># =&gt; []</span>
</span><span class='line'><span class="nb">p</span> <span class="n">b</span><span class="o">.</span><span class="n">bar</span>
</span><span class='line'><span class="c1"># =&gt; []</span>
</span></code></pre></td></tr></table></div></figure>


<p>Both objects, <strong>a</strong> and <strong>b</strong> share the same reference to the array instance created when <strong>a</strong> is initiated. There are a few ways to do a deep copy of an object, but that&#8217;s a different matter.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Rethinking web service development]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/06/13/rethinking-web-service-development/"/>
    <updated>2012-06-13T18:19:00-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/06/13/rethinking-web-service-development</id>
    <content type="html"><![CDATA[<p>While it&#8217;s true that there are still a lot of places where software isn&#8217;t
leveraged and many places where software needs to evolve, software is nearly everywhere!.
The type of software we write today needs to interact
with other software via some sort of network.
Any web developer out there is used to that, (s)he writes software that
runs on a server somewhere on the internet and users via a local software
(browser) consume their web applications using an internet connection.</p>

<p>What has changed in the last few years is that web applications start
providing more than just dynamically rendered HTML templates.
JavaScript is used to run more and more logic on the client side and JS
usually talks to some backends via JSON.
But web APIs are also more and more used to expose raw data to other
software. <strong>The challenge though, is that we haven&#8217;t changed the way we
write web applications to adapt to this new way of providing data.</strong>
In this article, I&#8217;d like to explore why and how we need to rethink web API development
and focus on communication and interaction.</p>

<h2>Unlearning</h2>

<p><a href="http://matt.aimonetti.net/images/matt_aimonetti-unlearn.jpeg"><img src="http://matt.aimonetti.net/images/matt_aimonetti-unlearn.jpeg"
  style="width:250px;display:block;margin:auto"/></a></p>

<p>Many are used to doing certain
things a certain way and it&#8217;s really hard to unlearn old habits. The more
concerning part of this fact is that it makes it <strong>hard for someone to step
back and evaluate honestly if a technical decision is due to comfort or
if it&#8217;s truly the right choice to achieve a given goal.</strong>
Ruby on Rails revolutionized the way we wrote web applications almost 10
years ago and since then, many web frameworks have adopted the Rails philosophy.
As a matter of fact, I personally think that when it comes to writing
front end applications, Rails is still one if not the best web
frameworks out there. But in its current state, I&#8217;m far from convinced that it&#8217;s a great tool to write
web APIs mainly because it was not designed for that and because it
comes with a lot of baggage.</p>

<h2>Focusing on the API</h2>

<p>Let&#8217;s take a step back and consider what is critical when developing a web
API:</p>

<ul>
<li><strong>Documentation</strong> for end users to know how to consume your services.</li>
<li><strong>Consistent and reliable</strong> output so you don&#8217;t break client applications
relying on your services.</li>
<li><strong>Maintainability</strong></li>
</ul>


<p>Note that I didn&#8217;t mention performance because I consider performance
almost always being important.
I also didn&#8217;t mention the whole
<a href="http://en.wikipedia.org/wiki/Representational_state_transfer">REST</a>/<a href="http://en.wikipedia.org/wiki/Remote_procedure_call">RPC</a>/<a href="http://en.wikipedia.org/wiki/Hypermedia">Hypstermedia</a> debate
since I consider it being an implementation detail and a totally orthogonal
discussion. But for the record, I personally don&#8217;t like solutions forcing you into a specific way of
providing your data (I&#8217;m looking at you <a href="http://wiki.basho.com/Webmachine.html">webmachine</a>).</p>

<h3>Documentation</h3>

<p>Documentation isn&#8217;t as important when you consume your own APIs because
you can look at your source code. However it gets much more tricky when
you start working with other teams. They might not know the
language/framework you use. They might not have time to go dig into your
source code to figure out what your <strong>meta-magical piece of clever
code</strong> does.</p>

<p>Lately more and more applications provide their raw data to the
outside world via web APIs. The developers who will consume your
resources don&#8217;t have access to your source code, probably don&#8217;t use the same
programming language and don&#8217;t have much time to guess how your API
work. Also your test suite won&#8217;t help communicate how your API works so
we need to find a different approach.
Also note that in this scenario, TDD/BDD won&#8217;t help us communicate
better and tests can&#8217;t be used as documentation since your tests aren&#8217;t
exposed.</p>

<p><em>Documentation is your #1 way to communicate with your human audience.</em>
Communication is key and even if as engineers we love to focus on code, if we
can&#8217;t communicate about it, end users will have a hard time using our
code and might just not even do it. <strong>The key is to communicate what your API does,
why someone might want to use it and how to use it.</strong></p>

<h3>Consistent and reliable output</h3>

<p>This point seems obvious but what we realize in reality is that
for many, API&#8217;s consistency and reliability doesn&#8217;t include
documentation. You find a lot of APIs out there poorly documented or out
of sync with the actual implementation.</p>

<p><strong>Documentation is a contract between
you: the developer and the other developers consuming your APIs.</strong>
As a developer, when you write any type of tests, they become some sort of quality contract.
If someone changes your code and break your tests, they break the implicit contract.
However, when talking about consuming data via an API, things get a bit more complicated.
We need a way to ensure that the end user expectations are matching our
implementation/documentation. Unit testing simply can&#8217;t guarantee that. Unit testing will
guarantee that the logic of your units of code is intact but it can&#8217;t
easily guarantee that the API output matches the documentation, however
this is something that has to be done.</p>

<h3>Maintainability</h3>

<p>This is a tricky point since maintainability is very subjective. But I
think we can agree that <a href="http://en.wikipedia.org/wiki/Decoupling#Software_development">decoupling</a>/<a href="http://en.wikipedia.org/wiki/Separation_of_concerns">separating concerns</a>
will make our code more maintainable.</p>

<p>That&#8217;s why I personally don&#8217;t think it&#8217;s a good idea to mix HTML
rendering code and web API code. Consider using different controllers or
different files depending on the way your code is structured.</p>

<p>I also strongly believe that the implementation details shouldn&#8217;t define
the way you design your web APIs. Don&#8217;t just slap a CRUD API on top of your
model and call it done. In most cases, you will pay a high price later
on if you take this approach because whenever you will need to change your
web API or your model, you will be stuck. This is because your interface
is now used by a lot of people and you can&#8217;t easily change it.
There are many ways to avoid API/model coupling, I don&#8217;t advocate one particularly, but whatever you do,
be sure you can make your models and APIs can evolve separately.</p>

<br><br>


<h2>My approach</h2>

<p>I&#8217;ve been designing and developing web APIs for many years and I&#8217;ve been
struggling with everything I mentioned until now.
I don&#8217;t claim that I&#8217;ve found <strong>the</strong> solution but I&#8217;d like to think that I
found one own way of addressing what are for me some of the most important parts
of API design.</p>

<h2>Being explicit</h2>

<p>I believe that there is value in being explicit in the way we describe
web APIs. Sometimes people get confused between being <a href="http://en.wiktionary.org/wiki/verbose">verbose</a> and being
<a href="http://en.wiktionary.org/wiki/explicit">explicit</a>. These are two different concepts.
Being explicit can sometimes seem verbose, but we need to evaluate the
value that can be extracted from the provided information. If there isn&#8217;t any clear value and too many words are used, then we
aren&#8217;t explicit, we are verbose.</p>

<p>When designing a web API, I don&#8217;t write it for myself, I do it for someone
else. <strong>It&#8217;s crucial to consider who you are writing for
and to expose what&#8217;s important for them.</strong>
A &#8220;small&#8221; problem is that we don&#8217;t show our code to our end users, so we
need to find a way to explicitly provide the important information and
to have this information provided to our end users.</p>

<h2>DSL</h2>

<p>For that I use a Domain Specific Language (DSL) which is a fancy way to
say that I have some specific code allowing me to explicitly define my
web services. These services exist as objects that can then be used to
<em>process requests</em> but also to <em>validate inputs</em>, and even more importantly
to <em>generate documentation</em>.</p>

<p>The DSL allows me to define the following:</p>

<ul>
<li>details about consuming the service (uri, HTTP verb, authentication details,
other service details&#8230;)</li>
<li>incoming params (which ones are allowed, the type, are they required,
optional?, what are they for)</li>
<li>service output</li>
</ul>


<p>The input details is really important to validate incoming requests and
reject them before even dispatching them. This is done for data sanity
and for security reason. It also defines a strong interface that is
easier to develop against and to maintain.</p>

<p>The output details might sound quite surprising and redundant. After all, isn&#8217;t that a
duplication of effort since we already have this information in the
&#8220;view&#8221;? Well, if we consider the important points I highlighted in the
first part of this article, we need a way to enforce a &#8220;contract&#8221;
between the end users and our implementation. To do that, we can&#8217;t
simply rely on our code since we can&#8217;t trust it. The output is important
to document the expected output but also to validate that our services
match our documentation and therefore our &#8220;contract&#8221;.</p>

<p>Here is an example of a Ruby <a href="https://github.com/mattetti/Weasel-Diesel">DSL</a> use to describe a hello world service:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe_service</span> <span class="s2">&quot;hello_world&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">service</span><span class="o">|</span>
</span><span class='line'>  <span class="n">service</span><span class="o">.</span><span class="n">formats</span>   <span class="ss">:json</span>
</span><span class='line'>  <span class="n">service</span><span class="o">.</span><span class="n">http_verb</span> <span class="ss">:get</span>
</span><span class='line'>  <span class="n">service</span><span class="o">.</span><span class="n">disable_auth</span> <span class="c1"># on by default</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># INPUT</span>
</span><span class='line'>  <span class="n">service</span><span class="o">.</span><span class="n">param</span><span class="o">.</span><span class="n">string</span>  <span class="ss">:name</span><span class="p">,</span> <span class="ss">:default</span> <span class="o">=&gt;</span> <span class="s1">&#39;World&#39;</span><span class="p">,</span> <span class="ss">:doc</span> <span class="o">=&gt;</span> <span class="s2">&quot;The name of the person to greet.&quot;</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># OUTPUT</span>
</span><span class='line'>  <span class="n">service</span><span class="o">.</span><span class="n">response</span> <span class="k">do</span> <span class="o">|</span><span class="n">response</span><span class="o">|</span>
</span><span class='line'>    <span class="n">response</span><span class="o">.</span><span class="n">object</span> <span class="k">do</span> <span class="o">|</span><span class="n">obj</span><span class="o">|</span>
</span><span class='line'>      <span class="n">obj</span><span class="o">.</span><span class="n">string</span> <span class="ss">:message</span><span class="p">,</span> <span class="ss">:doc</span> <span class="o">=&gt;</span> <span class="s2">&quot;The greeting message sent back. Defaults to &#39;World&#39;&quot;</span>
</span><span class='line'>      <span class="n">obj</span><span class="o">.</span><span class="n">datetime</span> <span class="ss">:at</span><span class="p">,</span> <span class="ss">:doc</span> <span class="o">=&gt;</span> <span class="s2">&quot;The timestamp of when the message was dispatched&quot;</span>
</span><span class='line'>    <span class="k">end</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># DOCUMENTATION</span>
</span><span class='line'>  <span class="n">service</span><span class="o">.</span><span class="n">documentation</span> <span class="k">do</span> <span class="o">|</span><span class="n">doc</span><span class="o">|</span>
</span><span class='line'>    <span class="n">doc</span><span class="o">.</span><span class="n">overall</span> <span class="s2">&quot;This service provides a simple hello world implementation example.&quot;</span>
</span><span class='line'>    <span class="n">doc</span><span class="o">.</span><span class="n">example</span> <span class="s2">&quot;&lt;code&gt;curl -I &#39;http://localhost:9292/hello_world?name=Matt&#39;&lt;/code&gt;&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<p>The DSL style isn&#8217;t really important, what&#8217;s important is that it allows
us to address some of the concerns we discussed earlier such as clearly
defined interface that can be communicated as documentation but also
acts as code. The focus is really on the API and everything fits in one
page. Because everything is an object and can be inspected, proper
and up to date documentation can be generated. And the implementation
can be tested against the documentation since everything is maintained
as code.</p>

<h2>Being agnostic</h2>

<p>While I like Ruby for many reasons, I don&#8217;t think that it&#8217;s the only
good language to implement great web services. It actually has its pros
and cons and so have all the web frameworks out there.
That&#8217;s why I wrote my DSL as a <a href="https://github.com/mattetti/Weasel-Diesel">standalone library</a> that could virtually
run on top of any web engine since it only outputs a representation of services.</p>

<p>While I hope to one day create an interesting interop solution across
programming language (by exporting the objects in a shared data
structure for instance), I started by focusing on the various Ruby web
frameworks.</p>

<p>The best starting point for me was to use <a href="http://www.sinatrarb.com/">Sinatra</a>.
I almost started just using <a href="http://rack.github.com/">rack</a>, but <a href="http://www.sinatrarb.com/">Sinatra</a> was providing me with a bit more feature for very
little headache and little code to grasp. I wrote <a href="https://github.com/mattetti/wd-sinatra">wd-sinatra</a> which
is a Ruby gem providing the <a href="https://github.com/mattetti/Weasel-Diesel">WeaselDiesel DSL</a> on top of a Sinatra app.
It comes with a generator and all the needed hooks to design, implement, test and
generate documentation for modern web APIs.</p>

<p>Using a simple rake command (Ruby&#8217;s version of make) one can generate
documentation or test the APIs against the implementations.
The &#8220;mini framework&#8221; is still very free form and should let you do
whatever you want. I don&#8217;t even set a default ORM for you to use since
this choice is highly personal. I do however leave you places to set
these things.</p>

<p><a href="http://matt.aimonetti.net/images/matt_aimonetti-WeaselDiesel_doc_generation.jpeg"><img src="http://matt.aimonetti.net/images/matt_aimonetti-WeaselDiesel_doc_generation.jpeg" style="width:200px;display:block;margin:auto" title="Matt Aimonetti - WeaselDiesel documentation generation example" alt="Matt Aimonetti - WeaselDiesel documentation generation example"></a></p>

<p>Sinatra and my &#8220;freedom framework&#8221; might seem too free form for
you. So I&#8217;m currently working on getting the DSL to run on top of Rails,
and by goal is to actually get it to run with a normal Rails app.</p>

<h2>Reconsidering Rails&#8217; MVC</h2>

<p>The Rails code base is very deeply marked with its own concept of MVC and having controllers and actions.
The challenge I have is that I like my service to be self contained. I
want my services to be simple and easy to grasp. I like having 1 service
per file. Models, libraries and other optional presenters/decorators live separately
but I like to have my service implementation code with the rest of my
service description. I honestly don&#8217;t like telling developers that they
need to go check a route file and that my simple service requires that
you open 12 files to understand what&#8217;s going on. Simpler is often better
and that&#8217;s why in <a href="https://github.com/mattetti/wd-sinatra">wd-sinatra</a>
the DSL and the implementation live together which is quite harder to do
with Rails.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">describe_service</span> <span class="s2">&quot;hello_world&quot;</span> <span class="k">do</span> <span class="o">|</span><span class="n">service</span><span class="o">|</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># [...] see the DSL section to see how the</span>
</span><span class='line'>  <span class="c1"># service is described. The block below is being</span>
</span><span class='line'>  <span class="c1"># being called in the context of the request</span>
</span><span class='line'>  <span class="c1"># after the request was validated.</span>
</span><span class='line'>
</span><span class='line'>  <span class="c1"># SERVICE IMPLEMENTATION</span>
</span><span class='line'>  <span class="c1"># the returned value is used as the response body, all the Sinatra helpers</span>
</span><span class='line'>  <span class="c1"># are available.</span>
</span><span class='line'>  <span class="n">service</span><span class="o">.</span><span class="n">implementation</span> <span class="k">do</span>
</span><span class='line'>    <span class="p">{</span><span class="ss">:message</span> <span class="o">=&gt;</span> <span class="s2">&quot;Hello </span><span class="si">#{</span><span class="n">params</span><span class="o">[</span><span class="ss">:name</span><span class="o">]</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">,</span> <span class="ss">:at</span> <span class="o">=&gt;</span> <span class="no">Time</span><span class="o">.</span><span class="n">now</span><span class="p">}</span><span class="o">.</span><span class="n">to_json</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Learning from experience</h2>

<p>While the DSL fits most of my needs, we all have different use cases.
While it&#8217;s critical for a library to have a clearly defined objective, it&#8217;s also important to
have many people help improve it. Part of the learning/vetting
excercise is to test an approach against different needs to define when
and why it works well in some cases and why sometinmes it doesn&#8217;t. This
allows us to define a sweet spot that should match the clearly defined
objective. However to be able to do that, a design needs to be tested by
many people. So far my approach seems to work very well when an
API needs to live outside an application and that 3rd parties need to
consume the resources. It also seems to work well with edge cases that
many API designers seem to encounter sooner or later.</p>

<h2>Conclusion</h2>

<p>At the end of the day, we have to remember that API stands for
<em>Application Programming Interface</em> and that these interfaces have to be
programmed so humans can write to comsume them. One of Ruby&#8217;s main design points has always
been to try to address human needs more than computer&#8217;s. As API
designers/implementers, it seems important to adopt the same approach and
consider who will use our interfaces.
I think the discussion should focus more on what to value when
defining web APIs, instead of arguing about how to implement APIs.
Standardization is a great concept but a really hard to implement. And
even with standards, we have to find a way to communicate with the API
users to express what standards we follow and where to find the various
entry points.
Think about your API, <strong>how well does it
communicate with your future API users</strong>, is it good enough for them to get
excited? Is it good enough for them to create something amazing with it?
If not, why not?</p>

<h3>Discussion</h3>

<p>Comments are available on <a href="http://news.ycombinator.com/item?id=4107126">this HackerNews thread</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[MacRuby on iOS - RubyMotion review]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/05/04/macruby-on-ios-rubymotion-review/"/>
    <updated>2012-05-04T07:17:47-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/05/04/macruby-on-ios-rubymotion-review</id>
    <content type="html"><![CDATA[<p>Yesterday, <a href="http://www.rubymotion.com/">RubyMotion</a> was released and let&#8217;s be honest, it is one the best alternatives to Objective-C out there (if not the best).</p>

<p>RubyMotion is a commercial, proprietary fork of MacRuby that targets iOS. This is not a small achievement, MacRuby relies on Objective C&#8217;s Garbage Collector (libauto) which is not available on iOS. Static compilation and new memory management solution was required to target the iOS platform . The new runtime had to be small and efficient. Furthermore, being able to run code on iOS isn&#8217;t enough, you need tools to interact with the compiler, to debug, to packages applications etc&#8230;</p>

<p>I don&#8217;t think anyone will contest the fact that RubyMotion is a well done product. The question however is, &#8221;<strong>is it worth for you to invest some money, time and energy in this product instead of using Apple&#8217;s language and tools</strong>&#8221;. In this article, I&#8217;ll try to balance the pros and cons of RubyMotion so you can have a better understanding of what RubyMotion could mean for you. As a disclaimer I should say that I was beta testing RubyMotion, that they are strong ties between RubyMotion and the MacRuby project I&#8217;m part of and finally that having MacRuby on iOS has been something I&#8217;ve been looking forward for a very long time.</p>

<p>Over the last few months I&#8217;ve seen RubyMotion take shape and finally hit the big 1.0. As you can see from <a href="http://twitter.com/#!/search/rubymotion?q=rubymotion">Twitter</a> and <a href="http://news.ycombinator.com/item?id=3924657">HackerNews</a>, the Ruby community is excited about being able to use their language to write statically compiled, native iOS apps. Spoiler alert, they are right, it&#8217;s a lot of fun.</p>

<p> </p>

<hr />

<p> </p>

<h2>What I like about RubyMotion:</h2>

<h3>Ruby Language</h3>

<p>I don&#8217;t mind Objective-C, I think it&#8217;s a fine superset of C, with the arrival of blocks, new literals and automatic memory management via ARC, Objective-C is actually getting better over time. But frankly, it&#8217;s not Ruby. You still have to deal with headers, you always have to compile your code via some weird Xcode voodoo settings, testing is a pain, the language, even with the new literals is quite verbose. On the other hand, using Ruby syntax I can get much more flexibility, reuse my code via mixins, easily reopen existing classes etc&#8230; At the end of the day, I end up with some code that seems cleaner, easier to understand and maintain even though I&#8217;m calling the same underlying APIs. Ruby&#8217;s flexibility also allows developers to make their own higher level APIs, take a look at some of the <a href="https://github.com/mattetti/BubbleWrap">wrappers/helpers</a> I wrote while playing with RubyMotion.</p>

<p><a href="http://www.ruby-lang.org/en/"><img src="http://merbist.com/wp-content/uploads/2012/05/matt_aimonetti-Ruby_logo-150x150.jpg" alt="Matt Aimonetti - Ruby Logo" /></a></p>

<h3>MacRuby</h3>

<p>RubyMotion is based on MacRuby, meaning that all the time and energy invested in the project will benefit RubyMotion&#8217;s users. All the concepts I explain in my <a href="http://shop.oreilly.com/product/0636920000723.do">MacRuby book</a> apply to RubyMotion. You don&#8217;t have to find workarounds to work with native APIs, Ruby objects are Objective-C objects and performance is great. I do regret Apple didn&#8217;t decide to embrace MacRuby for iOS but at the same time, even though we lost the Open Source aspect of the project and Apple&#8217;s backing, we gained much more flexibility and freedom on Laurent&#8217;s part.</p>

<p><a href="https://www.amazon.com/dp/1449380379?tag=merbist-20&amp;camp=213381&amp;creative=390973&amp;linkCode=as4&amp;creativeASIN=1449380379&amp;adid=1SKHT7ABMG1YJZ3136WQ&amp;"><img src="http://merbist.com/wp-content/uploads/matt_aimonetti/matt_aimonetti_macruby_book.gif" alt="" /></a></p>

<h3>REPL/Interactive shell</h3>

<p>RubyMotion doesn&#8217;t currently have a debugger, but it does have something Objective-C developers don&#8217;t have, a <a href="http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop">REPL</a> working with the simulator. This feature is quite handy when debugging your application or learning the Cocoa APIs. You can click on a visual element in the simulator and start modifying the objects in real time in a terminal window and see the modifications in the simulator. It reminds me of the first time I used firebug to edit the html/css of a web page and saw the changes in real time.</p>

<p><a href="http://www.youtube.com/watch?feature=player_embedded&amp;v=rejYKzLglSE#!"><img src="http://merbist.com/wp-content/uploads/2012/05/matt_aimonetti-RubyMotion-REPL-150x150.jpg" alt="Matt Aimonetti - RubyMotion REPL" /></a></p>

<h3>Not dependent on Xcode</h3>

<p>Xcode is fine when you write Objective-C code, but it crashes often, it has a complicated UI and never really worked well for MacRuby due to the fact that Objective-C and Ruby have different requirements and the that Xcode is not open source. It&#8217;s also fully controlled by Apple and doesn&#8217;t provide APIs for 3rd party developers. (That said, the Xcode team has often helped out when a new released of Xcode broke MacRuby, so thank you guys).</p>

<p>Being able to use simple rake tasks to compile, simulate and deploy applications is just really really nice. I&#8217;m sure we&#8217;ll end up with better IDE integration, nice GUIs for some who like that, but in the meantime, as a &#8220;hacker&#8221;, I really enjoy the simplicity of the Rake tasks and not being forced in using a specific IDE.</p>

<p> </p>

<h3>Memory management</h3>

<p>Even though ARC made memory management much easier for Objective-C developers, when using RubyMotion you don&#8217;t have to worry about memory (well at least not explicitly, don&#8217;t be dumb and create a bazillion objects and hold references to them either). This includes the CoreFoundation objects that you still have to manually manage in Objective-C. Memory management is transparent and in most cases it&#8217;s really nice.</p>

<p> </p>

<hr />

<p> </p>

<h2>What I like less about RubyMotion</h2>

<p>Here is a list of things that are cons to using RubyMotion, note that while the list is longer than my list of &#8220;pros&#8221;, I listed a lot of small things. I also think that most of these issues will get solved in the next few months.</p>

<p> </p>

<h3>Ruby language</h3>

<p>There are some cases where Ruby just isn&#8217;t that great or is not an option. Examples include dealing with API relying heavily on pointers, when using some of the lower level APIs or when you have to interact with C++ (video game engines for instance). The good news is that within the same project, you can write part of your code in Objective-C and the rest in RubyMotion. The other thing that bothers me a little bit with writing Ruby code for iOS is that you can&#8217;t easily enforce argument types and therefore you are losing a lot of the features provided by Clang to the Objective-C developers. I dream of an optionally typed Ruby &#8211; but that&#8217;s a different topic.</p>

<p>Another downside of using Ruby is that Ruby developers will assume all standard libraries and gems will be compatible with RubyMotion. This isn&#8217;t the case. You need to think of RubyMotion as only offering the Ruby syntax (modulo a few differences). To be honest, most of the std libs and gems aren&#8217;t that useful when writing iOS apps. Even when I write MacRuby apps, I rarely rely on them and pick libraries designed to work in a non-blocking, multi-threaded environment (usually ObjC libs that I wrap).</p>

<p> </p>

<h3>Cocoa Touch</h3>

<p>If you&#8217;re already an iOS/OS X developer, you know that most of the hurdles aren&#8217;t the language syntax but the Cocoa APIs. These APIs are what you need to interact with to create your application. Cocoa APIs are usually much lower-level compared to what you usually see in Python, Ruby or even Java. While they are quite consistent, the APIs still have a stiff learning curve and currently,  if you want to write iOS applications, even if you know Ruby, you still have to learn Cocoa.</p>

<p>However, I do think that with RubyMotion now building a userbase, we will start seeing more and more <a href="https://github.com/mattetti/BubbleWrap">wrappers</a> around these sometimes <a href="https://github.com/HipByte/RubyMotionSamples/blob/master/GestureTable/app/gesture_recognizer.rb">hideous APIs</a>.</p>

<p> </p>

<h3>No Xcode/IDE</h3>

<p>There are cases where an IDE is really practical, especially when learning new APIs. Being able to have code completion, quick access to the documentation, instrumentation, debugging, interface builder, refactoring tools are things that Objective-C developers might have a hard time with when switching to RubyMotion. If you don&#8217;t know either Ruby or Cocoa, getting started with RubyMotion might be quite hard and you are probably not currently in the target audience.</p>

<p> </p>

<h3>Writing UI code by hand</h3>

<p>In some cases, it makes sense, in other, it should be much easier. I know that Laurent is working on a DSL to make that easier and I&#8217;m looking forward to it. But in the mean time, this is quite a painful exercise, especially due to the complexity of the Cocoa UI APIs. Using Xcode&#8217;s interface builder and Storyboards is something I know a lot of us wish we could do with RubyMotion when developing specific types of applications.</p>

<p><a href="http://kurrytran.blogspot.fr/2011/07/simple-ios-5-tutorial-using-storyboard.html"><img src="http://merbist.com/wp-content/uploads/2012/05/matt_aimonetti_storyboard-1.jpg" alt="Matt Aimonetti - Xcode iOS storyboard" /></a></p>

<h3>No debugger</h3>

<p>Again, this is eventually coming but the current lack of debugger can be problematic at times, especially when the problem isn&#8217;t obvious.</p>

<p> </p>

<h3>Lack of clear target audience</h3>

<p>It&#8217;s hard to blame a brand new product for not having clearly defined a target audience. But as a developer I find myself wondering &#8220;when should I use RubyMotion and for what kinds of problems?&#8221; Is RubyMotion great for quick prototypes I can then turn into production code? Or is good for throw away prototypes? Is it reserved for &#8220;fart and flash light&#8221; applications? Is it ready for prime time and should I invest and write my new awesome apps using it? Should I convert over my existing code base over from Titanium (or whatever other alternatives you used)? Should I use RubyMotion every time I would use Objective-C?</p>

<p>I guess we will see when the first applications start hitting the app store and people start reporting on their experience.</p>

<h3>Documentation</h3>

<p>I&#8217;m partially to blame here since I could have moved my butt and start writing a book but the point is nonetheless valid. All the iOS documentation out there is for Objective-C, all the APIs and samples provided by Apple are obviously only for Objective-C. Thankfully, you can use the 2 MacRuby books available out there to understand how to convert this existing documentation into something useful, but RubyMotion will need to provide better and more adapted documentation for beginners. I have no doubt that this is coming sooner than later.</p>

<p> </p>

<h3>Proprietary solution</h3>

<p>RubyMotion isn&#8217;t open source and currently fully relies on the shoulders of a single man. If unfortunately, Laurent goes out of business or decides to do something else then we will have to rewrite our apps in Objective-C.  Using RubyMotion for a professional product represents a significant business risk, which is exactly the same as using proprietary technology from any vendor. Apple could also decide to switch to JavaScript or rewrite iOS in Java and deprecate Objective-C. Let&#8217;s just say that it is unlikely.</p>

<p>I usually favor open source solutions, from the programming language I use to the OS I deploy on. This isn&#8217;t always possible and if you want to write iOS applications, you don&#8217;t currently have a choice. I do wish Laurent had found a way to make money while keeping the source code open. But who knows &#8211; after he makes his first million(s), he might change his mind.</p>

<p><a href="http://merbist.com/wp-content/uploads/2012/05/matt_aimonetti-rms.jpg"><img src="http://merbist.com/wp-content/uploads/2012/05/matt_aimonetti-rms-150x150.jpg" alt="Matt Aimonetti - RMS" /></a></p>

<h2>Conclusion</h2>

<p>I would strongly suggest you consider giving RubyMotion a try. I can assure you that it will provide at least a few hours of &#8216;hacking fun&#8217; (and you will be able to brag about havng written your own iPhone app).  It will also help support financially someone who&#8217;s taking a risk in trying to push mobile development to the next level.</p>

<p>RubyMotion is, by far, my favorite alternative to Objective-C. But it is hard to tell, just 48 hours after its release, what people will do with it. Can it transcend the programming language barriers and attract Python, PHP, Java, ObjC and JavaScript developers? What is the sweet spot for RubyMotion applications? Will it affect the native vs web app battle? Can it make iOS development more accessible to the masses? Only time will tell.</p>

<br/>


<hr />

<h3>Update:</h3>

<p>Since RubyMotion 1.0 was released, I spent quite a lot of my free time leading the
development of <a href="http://bubblewrap.io/">BubbleWrap</a>, a free and open
source 3rd party library for <a href="http://www.rubymotion.com/">RubyMotion</a>.
Unfortunately, as of July 2011 <strong>I took a break from this project and
RubyMotion in general</strong>. I explained my motivations in <a href="https://groups.google.com/d/topic/rubymotion/XIE673vnuQk/discussion">this mailing list
post</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Getting started with mruby]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/04/25/getting-started-with-mruby/"/>
    <updated>2012-04-25T10:59:00-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/04/25/getting-started-with-mruby</id>
    <content type="html"><![CDATA[<p>mruby is the latest Ruby implementation in an already quite long list:</p>

<ul>
<li><a href="http://en.wikipedia.org/wiki/Ruby_MRI">MRI</a></li>
<li><a href="http://www.rubyenterpriseedition.com/">REE</a></li>
<li><a href="http://jruby.org/">JRuby</a></li>
<li><a href="http://rubini.us/">Rubinius</a></li>
<li><a href="http://macruby.org">MacRuby</a></li>
<li><a href="http://maglev.github.com/">Maglev</a></li>
<li><a href="http://www.ironruby.net/">IronRuby</a></li>
</ul>


<p>And many other less known implementations.</p>

<p>This time, the main man behind the project is the Ruby creator
himself: <a href="http://en.wikipedia.org/wiki/Yukihiro_Matsumoto">Yukihiro &#8216;Matz&#8217; Matsumoto</a>.
I already covered the <a href="http://matt.aimonetti.net/posts/2012/04/20/mruby-and-mobiruby/">announcement, you can read more about it there</a>.</p>

<h2>Why mruby?</h2>

<p>Following my previous <a href="http://matt.aimonetti.net/posts/2012/04/20/mruby-and-mobiruby/">article on mruby</a>, some people seemed to be confused by the &#8220;raison d&#8217;être&#8221;/purpose of the project and the difference between <a href="http://en.wikipedia.org/wiki/Ruby_MRI">MRI</a> and mruby.</p>

<p>mruby is designed to be modular and to be embedded, which means that
it&#8217;s expected to live inside other software applications. In other words
mruby&#8217;s goal is to make Ruby an embedded language. mruby is library that you link in other applications.</p>

<p>When talking about embedded languages, <a href="http://www.lua.org/">Lua</a> is
probably the most well known interpreted language.
<a href="http://www.lua.org/">Lua</a> is usually used for the purpose of offering a
higher level language (scripting language) inside a software written in a low level language
(usually C/C++). This is why <a href="http://www.lua.org/">Lua</a> is very popular
in the game industry to allow a scriptable interface and have parts of
the code interpreted instead of requiring to recompile the code base
against the target platform. According to <a href="http://www.satori.org/2009/03/the-engine-survey-general-results/">this survey from 2009</a>,
Lua was by far the most popular scripting language for game developers.</p>

<p><img src="http://www.satori.org/images/GE1/9Scripting.gif" alt="Lua vs other scripting languages" /></p>

<p>Of course, Lua is also popular outside of the game industry and the
reasons are simple to understand:</p>

<ul>
<li>very easily embedded (simple and well documented API)</li>
<li>small footprint</li>
<li>portable (runs almost everywhere)</li>
</ul>


<p>That gives you an idea of what mruby is trying to be. As a matter of
fact, this definition of Lua applies very well to mruby:</p>

<blockquote><p>&#8220;Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.&#8221;</p></blockquote>

<h2>Why not Lua?</h2>

<p>This is a matter of taste and requirements. It should eventually boil
down to the difference in the language designs.</p>

<p><img src='http://matt.aimonetti.net/images/lua_logo.gif' alt='Lua logo, Matt Aimonetti site' style='margin:auto;display:block;width:100px' class='no-caption'/></p>

<blockquote><p>Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics.</p></blockquote>

<p></br>
 <img src='http://matt.aimonetti.net/images/ruby_logo.png' alt='Ruby logo, Matt Aimonetti site' style='margin:auto;display:block;width:100px'class='no-caption' /></p>

<blockquote><p>Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.</p></blockquote>

<br style="clear:both">


<p>So on one hand you have a very simple language with a focus on
functional programming and on the other, a <a href="http://www.ruby-lang.org/en/about/">richer language</a> (albeit more
complex) with a
main focus on Object Oriented programming.</p>

<p>If you don&#8217;t know Lua, it&#8217;s quite close to JavaScript in the sense that
it&#8217;s a prototype based language and you can therefore write code that is
Object Oriented.</p>

<p>Ruby being a full OO and richer language, you can organize your code
differently and write more of your complex logic in a scriptable language.
One could even potentially leverage the language to create its own
Domain Specific Language in pure Ruby and create a &#8220;Rails like&#8221; experience
within its application.</p>

<p>To be honest, I don&#8217;t think that Ruby via mruby will just replace Lua,
but I do believe it will become a very interesting alternative for some
use cases. Especially if mruby manages to perform as well as Lua with the
same type of footprint. There is also the fact that mruby being
a sponsored project from the Japanese government, I wouldn&#8217;t be
surprised to see big Japanese companies experiment with embedding Ruby
in their devices. After all, software is being added in every object we
own, rapid development and being first to market is more critical than
ever which makes mruby is a very attractive solution.</p>

<h2>Getting started</h2>

<p>I wrote a simple hello world example to give you a place to start:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#include &lt;stdlib.h&gt;</span>
</span><span class='line'><span class="cp">#include &lt;stdio.h&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="cm">/* Include the mruby header */</span>
</span><span class='line'><span class="cp">#include &lt;mruby.h&gt;</span>
</span><span class='line'><span class="cp">#include &lt;mruby/compile.h&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="n">mrb_state</span> <span class="o">*</span><span class="n">mrb</span> <span class="o">=</span> <span class="n">mrb_open</span><span class="p">();</span>
</span><span class='line'>  <span class="kt">char</span> <span class="n">code</span><span class="p">[]</span> <span class="o">=</span> <span class="s">&quot;p &#39;hello world!&#39;&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Executing Ruby code with mruby!</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">mrb_load_string</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">code</span><span class="p">);</span>
</span><span class='line'>  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Or the longer/more complex version:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="cp">#include &lt;stdlib.h&gt;</span>
</span><span class='line'><span class="cp">#include &lt;stdio.h&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="cm">/* Include the mruby headers */</span>
</span><span class='line'><span class="cp">#include &lt;mruby.h&gt;</span>
</span><span class='line'><span class="cp">#include &lt;mruby/proc.h&gt;</span>
</span><span class='line'><span class="cp">#include &lt;mruby/data.h&gt;</span>
</span><span class='line'><span class="cp">#include &lt;mruby/compile.h&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">argv</span><span class="p">[])</span>
</span><span class='line'><span class="p">{</span>
</span><span class='line'>  <span class="k">struct</span> <span class="n">mrb_parser_state</span> <span class="o">*</span><span class="n">p</span><span class="p">;</span>
</span><span class='line'>  <span class="n">mrb_state</span> <span class="o">*</span><span class="n">mrb</span> <span class="o">=</span> <span class="n">mrb_open</span><span class="p">();</span>
</span><span class='line'>  <span class="kt">char</span> <span class="n">code</span><span class="p">[]</span> <span class="o">=</span> <span class="s">&quot;p &#39;hello world!&#39;&quot;</span><span class="p">;</span>
</span><span class='line'>  <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Executing code with mruby!</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'>  <span class="n">p</span> <span class="o">=</span> <span class="n">mrb_parse_string</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">code</span><span class="p">);</span>
</span><span class='line'>  <span class="kt">int</span> <span class="n">n</span><span class="p">;</span>
</span><span class='line'>  <span class="n">n</span> <span class="o">=</span> <span class="n">mrb_generate_code</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">p</span><span class="p">);</span>
</span><span class='line'>  <span class="n">mrb_run</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">mrb_proc_new</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">mrb</span><span class="o">-&gt;</span><span class="n">irep</span><span class="p">[</span><span class="n">n</span><span class="p">]),</span> <span class="n">mrb_top_self</span><span class="p">(</span><span class="n">mrb</span><span class="p">));</span>
</span><span class='line'>  <span class="k">if</span> <span class="p">(</span><span class="n">mrb</span><span class="o">-&gt;</span><span class="n">exc</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>      <span class="n">mrb_p</span><span class="p">(</span><span class="n">mrb</span><span class="p">,</span> <span class="n">mrb_obj_value</span><span class="p">(</span><span class="n">mrb</span><span class="o">-&gt;</span><span class="n">exc</span><span class="p">));</span>
</span><span class='line'>  <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>To compile and link the code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="err">$</span> <span class="n">gcc</span> <span class="o">-</span><span class="n">Iinclude</span> <span class="n">hello</span><span class="p">.</span><span class="n">c</span> <span class="n">lib</span><span class="o">/</span><span class="n">libmruby</span><span class="p">.</span><span class="n">a</span> <span class="o">-</span><span class="n">lm</span> <span class="o">-</span><span class="n">o</span> <span class="n">hello</span><span class="p">.</span><span class="n">out</span>
</span></code></pre></td></tr></table></div></figure>


<p>To execute it:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='c'><span class='line'><span class="err">$</span> <span class="p">.</span><span class="o">/</span><span class="n">hello</span><span class="p">.</span><span class="n">out</span>
</span><span class='line'><span class="n">Executing</span> <span class="n">Ruby</span> <span class="n">code</span> <span class="n">with</span> <span class="n">mruby</span><span class="o">!</span>
</span><span class='line'><span class="s">&quot;hello world!&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>To get started, you just need the <a href="https://github.com/mruby/mruby">mruby source code</a> and a compiler. (I haven&#8217;t tried to compile mruby or my sample on Windows for this code, but I assume it would work just fine with Visual C++).</p>

<p>The above example is very trivial and takes a line of Ruby code:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="nb">p</span> <span class="s1">&#39;hello world!&#39;</span>
</span></code></pre></td></tr></table></div></figure>


<p>that the linked mruby lib interprets.
You can take the compiled file and use it on other machines using the
same platform and the code will run fine, just like normal C code.</p>

<p>For a more complete example, take a look at the <a href="https://github.com/mruby/mruby/blob/master/tools/mruby/mruby.c">mruby&#8217;s standalone
interpreter</a></p>

<h2>Future</h2>

<p>It&#8217;s a bit too early to know if mruby will be successful or not. There are few things I will keep my
eyes on.</p>

<h3>Performance</h3>

<p>To be a true alternative to Lua, mruby will have to interpret Ruby code
much faster than what MRI is able to do right now. It will also need to
keep the memory footprint really tiny. Ruby being a more complicated
language than Lua, it might be tricky, but we&#8217;ll see.</p>

<h3>Documentation</h3>

<p>While the idea of using Ruby has a macro language might get a lot of
people excited, if documentation lacks or if the process is painful, the very same people might
fallback to another language.
Historically speaking, the MRI team has had issues with documentation
and communication due to various factors. I hope that thanks to Matz
experience and to the strong Ruby community, mruby will become a easy
and efficient alternative to Lua or people wanting to embed one of the
greatest interpreted languages.</p>

<h3>Ruby outside of Rails</h3>

<p>While <a href="http://rubyonrails.org">Ruby on Rails</a> really made Ruby popular,
I&#8217;m always a bit dissapointed when I see how many popular projects the
Ruby community has outside of Rails. Think about it, we have some
awesome implementation such as JRuby and MacRuby which allow you to use
an amazing amount of libraries to do the craziest thing one could
imagine using the language they like the most. But yet, Rails is still
by far the #1 Ruby project. Of course, there are many non-Rails related
projects out there, but they don&#8217;t benefit from Rails&#8217; aura.</p>

<p>What I hope with mruby is that it will allow developers to leverage the
beauty of Ruby and to create other niches for people to have fun.
Some people already started:</p>

<h4>MobiRuby</h4>

<p><a href="https://github.com/masuidrive">Yuichiro MASUI</a>
is working on having Ruby available on iOS and Android.</p>

<h4>Ruby for Node.js</h4>

<p><a href="http://mattn.kaoriya.net/">Yasuhiro Matsumoto</a> is working on <a href="https://github.com/mattn/mruby-uv">mruby-uv</a> an interface for <a href="https://github.com/joyent/libuv">libuv</a> Node.js&#8217; platform layer.</p>

<h4>mod_mruby</h4>

<p><a href="http://blog.matsumoto-r.jp/">MATSUMOTO Ryosuke</a> is working on an Apache
module for mruby called <a href="https://github.com/matsumoto-r/mod_mruby">mod_mruby</a> which would
be comparable to <a href="http://httpd.apache.org/docs/2.3/mod/mod_lua.html">mod_lua</a></p>

<h4>mruby REPL</h4>

<p><a href="">Frank Celler</a> started working and blogging about writing a REPL for
mruby, go check out <a href="http://www.avocadodb.org/category/mruby">his excellent blog posts</a> on the shell he&#8217;s working on and other stuff he&#8217;s doing with mruby.</p>

<p>But there is plenty more to do, <a href="http://antirez.com/post/scripting-branch-released.html">redis</a> for instance now has/is about to be scriptabled via Lua, what about trying to use mruby to also support Ruby?
What about scripting game logic and even full games in Ruby?
What about mruby on a <a href="http://www.raspberrypi.org/">Raspberry pi</a>?
Ruby on my TV, fridge, car, AC, solar panel controller&#8230;</p>

<p>I will certainly be looking forward to people trying to reproduce the success
of Rails in a different domain thanks to the Ruby language.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[mruby and MobiRuby]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/04/20/mruby-and-mobiruby/"/>
    <updated>2012-04-20T12:32:00-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/04/20/mruby-and-mobiruby</id>
    <content type="html"><![CDATA[<p>Today, two big Ruby news came directly from Japan:</p>

<ul>
<li>The Open Source release of <a href="http://en.wikipedia.org/wiki/Yukihiro_Matsumoto">Matz&#8217;</a> <a href="https://github.com/mruby/mruby">mruby on GitHub</a>.</li>
<li>The announce of <a href="http://mobiruby.org/">MobiRuby</a>, an upcoming solution
to develop iOS and Android applications using Ruby.</li>
</ul>


<p>Probably due to my involvement with the <a href="http://macruby.org/">MacRuby</a>
project, people have been asking me what I thought of these news.</p>

<h2>mruby</h2>

<p>mruby is far from being a new project. It&#8217;s based on the RiteVM which is a
sponsored project by the <a href="http://www.meti.go.jp/english/">Japanese ministry of Economy, Trade and Industry</a> and lead by Ruby&#8217;s creator: <a href="http://en.wikipedia.org/wiki/Yukihiro_Matsumoto">Yukihiro &#8220;Matz&#8221; Matsumoto</a> and was explained in details during <a href="http://www.slideshare.net/yukihiro_matz/rubyconf-2010-keynote-by-matz">Matz&#8217;s RubyConf 2010 keynote</a>.</p>

<p>Back in November 2011 Matz also explained mruby. His talk was recorded
and he explains very well the current Ruby ecosystem and why mruby makes
sense.</p>

<div class="video-container">
<iframe width="560" height="315" src="http://www.youtube.com/embed/sB-IifjyeLI" frameborder="0" allowfullscreen></iframe></div>


<p>As explained, the main goal of mruby is to have a Ruby version that can
be embedded and therefore have a smaller footprint, be compiled and
linked within another application.</p>

<p>Hiroshi Nakamura gave a great 1 line definition of mruby:</p>

<p><img src="http://matt.aimonetti.net/images/mruby_def.jpg" alt="mruby" /></p>

<p>mruby targets game developers (to use instead of Lua), embedded
application developers (devices, TV, phones..) and small memory
footprint server applications (instead of JS for instance).</p>

<p>I&#8217;m personally quite excited by mruby, it&#8217;s not there yet and there is
still a lot of work to do to prove the value of the project but it&#8217;s
certainly a great step in the right direction. What&#8217;s also really nice
is that the project is released under an OSS license allowing for all of
us to contribute and companies to improve the implementation based on
their own needs.</p>

<p><strong>Summary:</strong> mruby is a promising project even if it is still in its infancy.
Besides being yet another Ruby implementation, the fact that the
target audience and the project scope are well defined and that the project is lead
by Ruby&#8217;s author and sponsored by the Japanese government makes me want to believe that it can be a successful project. That said Lua is a simpler language and it is already well implemented in the targeted market, so hopefuly Matz, his team and the Japanese government have a plan to advocate and champion this new technology. Good luck to them and I&#8217;ll keep an attentive eye on the project.</p>

<p><strong>
Update: I wrote a <a href="http://matt.aimonetti.net/posts/2012/04/25/getting-started-with-mruby/">getting started with mruby</a> guide.
</strong></p>

<h2>MobiRuby</h2>

<p><a href="http://mobiruby.org/">MobiRuby</a> is being developed by <a href="https://github.com/masuidrive">Yuichiro MASUI</a> who works for <a href="http://www.appcelerator.com/">Appcelerator</a> the company behind the popular <a href="http://www.appcelerator.com/platform/titanium-sdk">Titanium platform</a> to write native iOS, Android apps in JS.</p>

<p>MobiRuby is built on top of mruby making it the first demonstration of
what motivated developers can do with Matz new implementation. Very much
like mruby, MobiRuby will be released under an OSS license but unlike
mruby, the <a href="http://www.apache.org/licenses/LICENSE-2.0.html">Apache license</a> was chosen.</p>

<p>So far this was just an announcement with a code sample and a
screenshot. That was enough to make the front page of <a href="http://news.ycombinator.com/item?id=3866418">HackerNews</a>. Apparently the author is planning on releasing a first version in a few months.</p>

<p> <img src="http://mobiruby.org/screenshot1.jpg" title="MobiRuby screenshot" alt="MobiRuby" /></p>

<p>It might surprise some, but I&#8217;m quite glad to see this kind of projects
even though, they compete to some extent against MacRuby. It proves two
things:</p>

<ul>
<li>there is a strong interest in having Ruby on mobile devices.</li>
<li>it&#8217;s technically possible to do so.</li>
</ul>


<p>Now, this is not something new either, Lua developers have been able to
write iOS apps for a while, yet the majority of the iOS developers still
use Objective-C. What are the challenges facing implementations trying
to replace Objective-C?</p>

<h3>The replacement language might not fit the Cocoa design.</h3>

<p>Developing an iOS/OS X app means that you spend your time using provided
libraries (called frameworks in Apple&#8217;s jargon). These frameworks have
specific patterns, a well defined syntax and usually work in a very
consistent/constraining way. Or your language is quite similar (like Ruby) and the
transition is easy, or you need to start writing and maintaining
wrappers (titanium).</p>

<h3>Bridged runtimes.</h3>

<p>Having 2 runtimes running at the same time is quite challenging and not
efficient. That&#8217;s one of the reasons why Apple pushed MacRuby to move from <a href="http://en.wikipedia.org/wiki/RubyCocoa">RubyCocoa</a> being a bridge and to have a Ruby implementation running in Objective-C runtime itself.
This allows something else, in MacRuby all objects are actually
Objective-C objects which means you don&#8217;t need to convert anything and
Cocoa APIs can be extended from Ruby code by just reopening them.</p>

<h3>Support.</h3>

<p>This one is critical for many. Often, you don&#8217;t want to have your next big
project rely on a technology that doesn&#8217;t have a good backing and
support. What happens if you build your app using an alternate
implementation and all a sudden the developer(s) get bored and move on,
or take another job?
What about the updates needed as Apple/Google update their platforms?
It might not be the best reason to not choose an alternative, but it&#8217;s
a reasonable reason especially for companies who want to be &#8220;safe&#8221;.</p>

<h3>Cocoa</h3>

<p>Cocoa APIs represent probably 90% of the challenge when writing iOS/OS X
applications. The APIs, while powerful and efficient, are often a pain
to get used to and to learn.</p>

<p>You have the challenge of the documentation and the examples that
are only in Objective-C, requiring that someone <a href="http://www.amazon.com/gp/product/1449380379/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1449380379">writes a book</a> and/or that
you convert and maintain an enormous amount of documentation.</p>

<p>You also have all the tools provided by Apple which, you often can&#8217;t
fully use because you aren&#8217;t using their toolchain.</p>

<p>To be honest, after so many years using MacRuby, I think that the real
value of such a project isn&#8217;t in the easier syntax but instead in the
fact that you can easily build wrappers and higher level interfaces
around repetitive tasks. Having a mix of a well designed DSL and yet
access to the native object is something extremely powerful.</p>

<h3>Objective-C is evolving.</h3>

<p>Objective-C is evolving, with the introduction of <a href="http://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html">ARC</a>, memory management became much easier. The latest version of clang also granted Objective-C with a nicer syntax thanks to new literals and object subscripting (<a href="http://clang.llvm.org/docs/ObjectiveCLiterals.html">read more</a>). As a matter of fact, Objective-C&#8217;s syntax is getting closer and closer to Ruby&#8217;s making the choice to use an alternate much harder.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
</pre></td><td class='code'><pre><code class='objective-c'><span class='line'><span class="c1">// character literals.</span>
</span><span class='line'><span class="n">NSNumber</span> <span class="o">*</span><span class="n">theLetterZ</span> <span class="o">=</span> <span class="sc">@&#39;Z&#39;</span><span class="p">;</span>          <span class="c1">// equivalent to [NSNumber numberWithChar:&#39;Z&#39;]</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// integral literals.</span>
</span><span class='line'><span class="n">NSNumber</span> <span class="o">*</span><span class="n">fortyTwo</span> <span class="o">=</span> <span class="err">@</span><span class="mi">42</span><span class="p">;</span>             <span class="c1">// equivalent to [NSNumber numberWithInt:42]</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// floating point literals.</span>
</span><span class='line'><span class="n">NSNumber</span> <span class="o">*</span><span class="n">piDouble</span> <span class="o">=</span> <span class="err">@</span><span class="mf">3.1415926535</span><span class="p">;</span>   <span class="c1">// equivalent to [NSNumber numberWithDouble:3.1415926535]</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// BOOL literals.</span>
</span><span class='line'><span class="n">NSNumber</span> <span class="o">*</span><span class="n">yesNumber</span> <span class="o">=</span> <span class="err">@</span><span class="n">YES</span><span class="p">;</span>           <span class="c1">// equivalent to [NSNumber numberWithBool:YES]</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Container literals</span>
</span><span class='line'><span class="n">NSArray</span> <span class="o">*</span><span class="n">array</span> <span class="o">=</span> <span class="err">@</span><span class="p">[</span> <span class="s">@&quot;Hello&quot;</span><span class="p">,</span> <span class="n">NSApp</span><span class="p">,</span> <span class="p">[</span><span class="n">NSNumber</span> <span class="nl">numberWithInt:</span><span class="mi">42</span><span class="p">]</span> <span class="p">];</span>
</span><span class='line'><span class="kt">id</span> <span class="n">value</span> <span class="o">=</span> <span class="n">array</span><span class="p">[</span><span class="n">idx</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSDictionary</span> <span class="o">*</span><span class="n">dictionary</span> <span class="o">=</span> <span class="err">@</span><span class="p">{</span>
</span><span class='line'>  <span class="s">@&quot;name&quot;</span> <span class="o">:</span> <span class="n">NSUserName</span><span class="p">(),</span>
</span><span class='line'>  <span class="s">@&quot;date&quot;</span> <span class="o">:</span> <span class="p">[</span><span class="n">NSDate</span> <span class="n">date</span><span class="p">],</span>
</span><span class='line'>  <span class="s">@&quot;processInfo&quot;</span> <span class="o">:</span> <span class="p">[</span><span class="n">NSProcessInfo</span> <span class="n">processInfo</span><span class="p">]</span>
</span><span class='line'><span class="p">};</span>
</span><span class='line'><span class="kt">id</span> <span class="n">oldObject</span> <span class="o">=</span> <span class="n">dictionary</span><span class="p">[</span><span class="n">key</span><span class="p">];</span>
</span><span class='line'><span class="n">dictionary</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">newObject</span><span class="p">;</span> <span class="c1">// replace oldObject with newObject</span>
</span></code></pre></td></tr></table></div></figure>


<h3>Performance.</h3>

<p>Even though devices are more and more powerful, performance is often
critical and Apple optimized the performance of their solution for their
language. If you have ever developed a Titanium app, you know that it
can be an issue and you might have to find workarounds to get decent
performance.</p>

<p><strong>Summary:</strong> Based on all these things, once MobiRuby will be released, I will be
able to make a better judgement. But based on what I have seen so far,
I&#8217;m quite concerned by the syntax and the performance we will get out of
the box. But time will tell and things can always be improved.
Ruby on iOS/Android is something exciting and I&#8217;m looking forward to
testing the first betas.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Building and implementing a Single Sign-On solution]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/04/04/building-and-implementing-a-single-sign-on-solution/"/>
    <updated>2012-04-04T07:29:16-07:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/04/04/building-and-implementing-a-single-sign-on-solution</id>
    <content type="html"><![CDATA[<p>Most modern web applications start as a monolithic code base and, as complexity increases, the once small app gets split apart into many &#8220;modules&#8221;. In other cases, engineers opt for a <a href="http://en.wikipedia.org/wiki/Service-oriented_architecture">SOA</a> design approach from the beginning. One way or another, we start running multiple separate applications that need to interact seamlessly. My goal will be to describe some of the high-level challenges and solutions found in implementing a Single-Sign-On service.</p>

<h2>Authentication vs Authorization</h2>

<p>I wish these two words didn&#8217;t share the same root because it surely confuses a lot of people. My most frequently-discussed example is <a href="http://en.wikipedia.org/wiki/OAuth">OAuth</a>. Every time I start talking about implementing a centralized/unified authentication system, someone jumps in and suggests that we use <a href="http://en.wikipedia.org/wiki/OAuth">OAuth</a>. The challenge is that <a href="http://en.wikipedia.org/wiki/OAuth">OAuth</a> is an authorization system, not an authentication system.</p>

<p>It&#8217;s tricky, because you might actually be &#8220;authenticating&#8221; yourself to website X using OAuth. What you are really doing is allowing website X to use your information stored by the OAuth provider. It is true that OAuth offers a pseudo-authentication approach via its provider but that is not the main goal of <a href="http://en.wikipedia.org/wiki/OAuth">OAuth</a>: the Auth in OAuth stands for Authorization, not Authentication.</p>

<p>Here is how we could briefly describe each role:</p>

<ul>
<li><p><strong>Authentication</strong>: recognizes who you are.</p></li>
<li><p><strong>Authorization</strong>: know what you are allowed to do, or what you allow others to do.</p></li>
</ul>


<p>If you are feel stuck in your design and something seems wrong, ask yourself if you might be confused by the 2 auth words. This article will only focus on <strong>authentication</strong>.</p>

<h2>A Common Scenario</h2>

<p><a href="http://merbist.com/wp-content/uploads/2012/04/SSO-simplescenario.png"><img src="http://merbist.com/wp-content/uploads/2012/04/SSO-simplescenario.png" alt="SSO diagram with 3 top applications connecting to an authorization service." /></a></p>

<p>This is probably the most common structure, though I made it slightly more complex by drawing the three main apps in different programming languages. We have three web applications running on different subdomains and sharing account data via a centralized authentication service.</p>

<p><strong>Goals:</strong></p>

<ul>
<li><p>Keep authentication and basic account data isolated.</p></li>
<li><p>Allow users to stay logged in while browsing different apps.</p></li>
</ul>


<p>Implementing such a system should be easy. That said, if you migrate an existing app to an architecture like that, you will spend 80% of your time decoupling your legacy code from authentication and wondering what data should be centralized and what should be distributed. Unfortunately, I can&#8217;t tell you what to do there since this is very domain specific. Instead, let&#8217;s see how to do the &#8220;easy part.&#8221;</p>

<h2>Centralizing and Isolating Shared Account Data</h2>

<p>At this point, you more than likely have each of your apps talk directly to shared database tables that contain user account data. The first step is to migrate away from doing that. We need a single interface that is the only entry point to create or update shared account data. Some of the data we have in the database might be app specific and therefore should stay within each app, anything that is shared across apps should be moved behind the new interface.</p>

<p>Often your centralized authentication system will store the following information:</p>

<ul>
<li><p>ID</p></li>
<li><p>first name</p></li>
<li><p>last name</p></li>
<li><p>login/nickname</p></li>
<li><p>email</p></li>
<li><p>hashed password</p></li>
<li><p>salt</p></li>
<li><p>creation timestamp</p></li>
<li><p>update timestamp</p></li>
<li><p>account state (verified, disabled &#8230;)</p></li>
</ul>


<p>Do not duplicate this data in each app, instead have each app rely on the account ID to query data that is specific to a given account in the app. Technically that means that instead of using SQL joins, you will query your database using the ID as part of the condition.</p>

<p>My suggestion is to do things slowly but surely. Migrate your database schema piece by piece assuring that everything works fine. Once the other pieces will be in place, you can migrate one code API a time until your entire code base is moved over. You might want to change your DB credentials to only have read access, then no access at all.</p>

<h2>Login workflow</h2>

<p>Each of our apps already has a way for users to login. We don&#8217;t want to change the user experience, instead we want to make a transparent modification so the authentication check is done in a centralized way instead of a local way. To do that, the easiest way is to keep your current login forms but instead of POSTing them to your local apps, we&#8217;ll POST them to a centralized authentication API. (SSL is strongly recommended)</p>

<p><a href="http://merbist.com/wp-content/uploads/2012/04/SSO-login.png"><img src="http://merbist.com/wp-content/uploads/2012/04/SSO-login.png" alt="diagram showing the login workflow" /></a></p>

<p>As shown above, the login form now submits to an endpoint in the authentication application. The form will more than likely include a login or email and a clear text password as well as a hidden callback/redirect url so that the authentication API can redirect the user&#8217;s browser to the original app. For security reasons, you might want to white list the domains you allow your authentication app to redirect to.</p>

<p>Internally, the Authentication app will validate the identifier (email or login) using a hashed version of the clear password against the matching record in the account data. If the verification is successful, a token will be generated containing some user data (for instance: id, first name, last name, email, created date, authentication timestamp). If the verification failed, the token isn&#8217;t generated. Finally the user&#8217;s browser is redirected to the callback/redirect URL provided in the request with the token being passed.</p>

<p>You might want to safely encrypt the data in a way that allows the clients to verify and trust that the token comes from a trusted source. A great solution for that would be to use <a href="http://en.wikipedia.org/wiki/RSA_(algorithm">RSA encryption</a>) with the public key available in all your client apps but the private key only available on the auth server(s). Other strong encryption solutions would also work. For instance, another appropriate approach would be to add a signature to the params sent back. This way the clients could check the authenticity of the params. <a href="http://en.wikipedia.org/wiki/HMAC">HMAC</a> or <a href="http://en.wikipedia.org/wiki/Digital_Signature_Algorithm">DSA</a> signature are great for that but in some cases, you don&#8217;t want people to see the content of the data you send back. That&#8217;s especially true if you are sending back a &#8216;mobile&#8217; token for instance. But that&#8217;s a different story. What&#8217;s important to consider is that we need a way to ensure that the data sent back to the client can&#8217;t be tampered with. You might also make sure you prevent replay attacks.</p>

<p>On the other side, the application receives a GET request with a token param. If the token is empty or can&#8217;t be decrypted, authentication failed. At that point, we need to show the user the login page again and let him/her try again. If on the other hand, the token can be decrypted, the content should be saved in the session so future requests can reuse the data.</p>

<p>We described the authentication workflow, but if a user logins in application X, (s)he won&#8217;t be logged-in in application Y or Z. The trick here, is to set a top level domain cookie that can be seen by all applications running on subdomains. Certainly, this solution only works for apps being on the same domain, but we&#8217;ll see later how to handle apps on different domains.</p>

<p><a href="http://merbist.com/wp-content/uploads/2012/04/SSO-login-cookie.png"><img src="http://merbist.com/wp-content/uploads/2012/04/SSO-login-cookie.png" alt="" /></a></p>

<p>The cookie doesn&#8217;t need to contain a lot of data, its value can contain the account id, a timestamp (to know when authentication happened and a trusted signature) and a signature. The signature is critical here since this cookie will allow users to be automatically logged in other sites. I&#8217;d recommend the  <a href="http://en.wikipedia.org/wiki/HMAC">HMAC</a> or <a href="http://en.wikipedia.org/wiki/Digital_Signature_Algorithm">DSA</a> encryptions to generate the signature. The DSA encryption, very much like the RSA encryption is an asymmetrical encryption relying on a public/private key. This approach offers more security than having something based a shared secret like HMAC does. But that&#8217;s really up to you.</p>

<p>Finally, we need to set a filter in your application. This auto-login filter will check the presence of an auth cookie on the top level domain and the absence of local session. If that&#8217;s the case, a session is automatically created using the user id from the cookie value after the cookie integrity is verified. We could also share the session between all our apps, but in most cases, the data stored by each app is very specific and it&#8217;s safer/cleaner to keep the sessions isolated. The integration with an app running on a different service will also be easier if the sessions are isolated.</p>

<p> </p>

<h2>Registration</h2>

<p>For registration, as for login, we can take one of two approaches: point the user&#8217;s browser to the auth API or make S2S (server to server) calls from within our apps to the Authentication app. POSTing a form directly to the API is a great way to reduce duplicated logic and traffic on each client app so I&#8217;ll demonstrate this approach.</p>

<p><a href="http://merbist.com/wp-content/uploads/2012/04/CopyofSSO-register.png"><img src="http://merbist.com/wp-content/uploads/2012/04/CopyofSSO-register.png" alt="" /></a></p>

<p>As you can see, the approach is the same we used to login. The difference is that instead of returning a token, we just return some params (id, email and potential errors). The redirect/callback url will also obviously be different than for login. You could decide to encrypt the data you send back, but in this scenario, what I would do is set an auth cookie at the .domain.com level when the account is created so the &#8220;client&#8221; application can auto-login the user. The information sent back in the redirect is used to re-display the register form with the error information and the email entered by the user.</p>

<p>At this point, our implementation is almost complete. We can create an account and login using the defined credentials. Users can switch from one app to another without having to re login because we are using a shared signed cookie that can only be created by the authentication app and can be verified by all &#8220;client&#8221; apps. Our code is simple, safe and efficient.</p>

<h2>Updating or deleting an account</h2>

<p>The next thing we will need is to update or delete an account. In this case, this is something that needs to be done between a &#8220;client&#8221; app and the authentication/accounts app. We&#8217;ll make S2S (server to server) calls. To ensure the security of our apps and to offer a nice way to log requests, API tokens/keys will be used by each client to communicate with the authentication/accounts app. The API key can be passed using a <a href="http://en.wikipedia.org/wiki/List_of_HTTP_header_fields">X-header</a> so this concern stays out of the request params and our code can process separately the authentication via X-header and the actual service implementation. S2S services should have a filter verifying and logging the API requests based on the key sent with the request. The rest is straight forward.</p>

<h2>Using different domains</h2>

<p>Until now, we assumed all our apps were on the same top domain. In reality, you will often find yourself with apps on different domains. This means that you can&#8217;t use the shared signed cookie approach anymore. However, there is a simple trick that will allow you to avoid requiring your users to re-login as they switch apps.</p>

<p><a href="http://merbist.com/wp-content/uploads/2012/04/SSO-differentdomains-1.png"><img src="http://merbist.com/wp-content/uploads/2012/04/SSO-differentdomains-1.png" alt="" /></a></p>

<p> </p>

<p>The trick consists, when a local session isn&#8217;t present, of using an iframe in the application using the different domain. The iframe loads a page from the authentication/accounts app which verifies that a valid cookie was set on the main top domain. If that is the case, we can tell the application that the user is already globally logged in and we can tell the iframe host to redirect to an application end point passing an auth token the same way we did during the authentication. The app would then create a session and redirect the user back to where (s)he started. The next requests will see the local session and this process will be ignored.</p>

<p>If the authentication application doesn&#8217;t find a signed cookie, the iframe can display a login form or redirect the iframe host to a login form depending on the required behavior.</p>

<p>Something to keep in mind when using multiple apps and domains is that you need to keep the shared cookies/sessions in sync, meaning that if you log out from an app, you need to also delete the auth cookie to ensure that users are globally logged out. (It also means that you might always want to use an iframe to check the login status and auto-logoff users).</p>

<p> </p>

<h2>Mobile clients</h2>

<p>Another part of implementing a SSO solution is to handle mobile clients. Mobile clients need to be able to register/login and update accounts. However, unlike S2S service clients, mobile clients should only allow calls to modify data on the behalf of a given user. To do that, I recommend providing opaque mobile tokens during the login process. This token can then be sent with each request in a X-header so the service can authenticate the user making the request. Again, SSL is strongly recommended.</p>

<p>In this approach, we don&#8217;t use a cookie and we actually don&#8217;t need a SSO solution, but an unified authentication system.</p>

<p> </p>

<h2>Writing web services</h2>

<p>Our Authentication/Accounts application turns out to be a pure web API app.</p>

<p>We also have 3 sets of APIs:</p>

<ul>
<li><p>Public APIs: can be accessed from anywhere, no authentication required</p></li>
<li><p>S2S APIs: authenticated via API keys and only available to trusted clients</p></li>
<li><p>Mobile APIs: authenticated via a mobile token and limited in scope.</p></li>
</ul>


<p>We don&#8217;t need dynamic HTML views, just simple web service related code. While this is a little bit off topic, I&#8217;d like to take a minute to show you how I personally like writing web service applications.</p>

<p>Something that I care a lot about when I implement web APIs is to validate incoming params. This is an opinionated approach that I picked up while at Sony and that I think should be used every time you implement a web API. As a matter of fact, I wrote a Ruby <a href="https://github.com/mattetti/Weasel-Diesel">DSL library (Weasel Diesel)</a> allowing you <a href="https://github.com/mattetti/sinatra-web-api-example/blob/master/api/hello_world.rb">describe a given service</a>, its <a href="https://github.com/mattetti/sinatra-web-api-example/blob/master/api/hello_world.rb#L7">incoming params</a>, and the <a href="https://github.com/mattetti/sinatra-web-api-example/blob/master/api/hello_world.rb#L10-15">expected output</a>. This DSL is hooked into a web backend so you can implement services using a web engine such as <a href="http://www.sinatrarb.com/">Sinatra</a> or maybe Rails3. Based on the DSL usage, incoming parameters are be verified before being processed. The other advantage is that you can generate documentation based on the API description as well as automated tests.</p>

<p>You might be familiar with <a href="https://github.com/intridea/grape">Grape</a>, another DSL for web services. Besides the obvious style difference <a href="https://github.com/mattetti/Weasel-Diesel">Weasel Diesel </a>offers the following advantages:</p>

<ul>
<li><p>input validation/sanitization</p></li>
<li><p>service isolation</p></li>
<li><p>generated documentation</p></li>
<li><p>contract based design</p></li>
</ul>


<p>Here is a hello world webservice being implemented using Weasel Diesel and Sinatra:</p>

<div><script src='https://gist.github.com/2300131.js?file='></script>
<noscript><pre><code>describe_service &quot;hello_world&quot; do |service|
  service.formats   :json
  service.http_verb :get
  service.disable_auth # on by default

  # INPUT
  service.param.string  :name, :default =&gt; 'World'

  # OUTPUT
  service.response do |response|
    response.object do |obj|
        obj.string :message, :doc =&gt; &quot;The greeting message sent back. Defaults to 'World'&quot;
      obj.datetime :at, :doc =&gt; &quot;The timestamp of when the message was dispatched&quot;
      end
  end

  # DOCUMENTATION
  service.documentation do |doc|
    doc.overall &quot;This service provides a simple hello world implementation example.&quot;
    doc.param :name, &quot;The name of the person to greet.&quot;
    doc.example &quot;&lt;code&gt;curl -I 'http://localhost:9292/hello_world?name=Matt'&lt;/code&gt;&quot;
 end

  # ACTION/IMPLEMENTATION
  service.implementation do
    {:message =&gt; &quot;Hello #{params[:name]}&quot;, :at =&gt; Time.now}.to_json
  end

end</code></pre></noscript></div>


<p>Basis test validating the contract defined in the DSL and the actual output when the service is called:</p>

<div><script src='https://gist.github.com/2300440.js?file='></script>
<noscript><pre><code>class HelloWorldTest &lt; MiniTest::Unit::TestCase

  def test_response
    TestApi.get &quot;/hello_world&quot;, :name =&gt; 'Matt'
    assert_api_response
  end

end</code></pre></noscript></div>


<p>Generated documentation:</p>

<p><img src="https://img.skitch.com/20120404-t1j93b73tef5pmd5idfqqa61td.jpg" alt="" /></p>

<p>If the DSL and its features seem appealing to you and you are interested in digging more into it, the easiest way is to fork <a href="https://github.com/mattetti/sinatra-web-api-example/">this demo repo</a> and start writing your own services.</p>

<p>The DSL has been used in production for more than a year, but there certainly are tweaks and small changes that can make the user experience even better. Feel free to fork the <a href="https://github.com/mattetti/Weasel-Diesel">DSL repo</a> and send me Pull Requests.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Learning from Rails' failures]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/02/29/learning-from-rails-failures/"/>
    <updated>2012-02-29T07:48:08-08:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/02/29/learning-from-rails-failures</id>
    <content type="html"><![CDATA[<p>Ruby on Rails undisputedly changed the way web frameworks are designed. Rails became a reference when it comes to leveraging conventions, easy baked in feature set and a rich ecosystem. However, I think that Rails did and still does a lot of things pretty poorly.  By writing this post, I&#8217;m not trying to denigrate Rails, there are many other people out there already doing that. My hope is that by listing what I think didn&#8217;t and still doesn&#8217;t go well, we can learn from our mistakes and improve existing solutions or create better new ones.</p>

<p><a href="http://merbist.com/2012/02/29/learning-from-rails-failures/train_fail/"><img src="http://merbist.com/wp-content/uploads/2012/02/train_fail-300x188.jpg" alt="" /></a></p>

<h2>Migration/upgrades</h2>

<p>Migrating a Rails App from a version to the other is very much like playing the lottery, you are almost sure you will lose. To be more correct, you know things will break, you just don&#8217;t know what, when and how. The Rails team seems to think that everybody is always running on the cutting edge version and don&#8217;t consider people who prefer to stay a few version behind for stability reasons. What&#8217;s worse is that plugins/gems might or might not compatible with the version you are updating to, but you will only know that by trying yourself and letting others try and report potential issues.</p>

<p>This is for me, by far, the biggest issue with Rails and something that should have been fixed a long time ago. If you&#8217;re using the WordPress blog engine, you know how easy and safe it is to upgrade the engine or the plugins. Granted WordPress isn&#8217;t a web dev framework, but it gives you an idea of what kind of experience we should be striving for.</p>

<p> </p>

<h2>Stability vs playground zone</h2>

<p>New features are cool and they help make the platform more appealing to new comers. They also help shape the future of a framework. But from my perspective, that shouldn&#8217;t come to the cost of stability. Rails 3&#8217;s new asset pipeline is a good example of a half-baked solution shoved in a release at the last minute and creating a nightmare for a lot of us trying to upgrade. I know, I know, you can turn off the asset pipeline and it got better since it was first released. But shouldn&#8217;t that be the other way around? Shouldn&#8217;t fun new ideas risking the stability of an app or making migration harder, be off by default and turned on only by people wanting to experiment? When your framework is young, it&#8217;s normal that you move fast and sometimes break, but once it matures, these things shouldn&#8217;t happen.</p>

<p> </p>

<h2>Public/private/plugin APIs</h2>

<p>This is more of a recommendation than anything else. When you write a framework in a very dynamic language like Ruby, people will &#8220;monkey patch&#8221; your code to inject features. Sometimes it is due to software design challenges, sometimes it&#8217;s because people don&#8217;t know better. However,  by not explicitly specifying what APIs are private (they can change at anytime, don&#8217;t touch), what APIs are public (stable, will be slowly deprecated when they need to be changed) and which ones are for plugin devs only (APIs meant for instrumentation, extension etc..), you are making migration to newer versions much harder. You see, if you have a small, clean public API, then it&#8217;s easy to see what could break, warn developers and avoid migration nightmares. However, you need to start doing that early on in your project, otherwise you will end up like Rails where all code can potentially change anytime.</p>

<p> </p>

<h2>Rails/Merb merge was a mistake</h2>

<p>This is my personal opinion and well, feel free to disagree, nobody will ever be able to know to for sure. Without explaining what happened behind closed doors and the various personal motivations, looking at the end result, I agree with the group of people thinking that the merge didn&#8217;t turn up to be a good thing. For me, Rails 3 isn&#8217;t significantly better than Rails 2 and it took forever to be released. You still can&#8217;t really run a mini Rails stack like promised. I did hear that Strobe (company who was hiring Carl Lerche, Yehuda Katz and contracted Jose Valim) used to have an ActionPack based, mini stack but it was never released and apparently only Rails core members really knew what was going on there. Performance in vanilla Rails 3 are only now getting close to what you had with Rails 2 (and therefore far from the perf you were getting with Merb). Thread-safety is still OFF by default meaning that by default your app uses a giant lock only allowing a process to handle 1 request at a time. For me, the flexibility and performance focus of Merb were mainly lost in the merge with Rails. (Granted, some important things such as ActiveModel, cleaner internals and others have made their way into Rails 3)</p>

<p>But what&#8217;s worse than everything listed so far is that the lack of competition and the internal rewrites made Rails lose its headstart.  Rails is very much HTML/view focused, its primarily strength is to make server side views trivial and it does an amazing job at that. But let&#8217;s be honest, that&#8217;s not the future for web dev. The future is more and more logic pushed to run on the client side (in JS) and the server side being used as an API serving data for the view layer. I&#8217;m sorry but adding support for CoffeeScript doesn&#8217;t really do much to making Rails evolve ahead of what it currently is. Don&#8217;t get me wrong, I&#8217;m a big fan of CoffeeScript, that said I still find that Rails is far from being optimized to developer web APIs in Rails. You can certainly do it, but you are basically using a tool that wasn&#8217;t designed to write APIs and you pay the overhead for that. If there is one thing I wish Rails will get better at is to make writing pure web APIs better (thankfully there is Sinatra). But at the end of the day, I think that two projects with different philosophies and different approaches are really hard to merge, especially in the open source world. I wouldn&#8217;t go as far as saying like others that Rails lost its sexiness to node.js because of the wasted time, but I do think that things would have been better for all if that didn&#8217;t happen. However, I also have to admit that I&#8217;m not sure how much of a big deal that is. I prefer to leave the past behind, learn from my own mistake and move on.</p>

<p> </p>

<h2>Technical debts</h2>

<p>Here I&#8217;d like to stop to give a huge props to Aaron &#8221;<a href="http://twitter.com/tenderlove">@tenderlove</a>&#8221; Patterson, the man who&#8217;s actively working to reduce the <a href="http://en.wikipedia.org/wiki/Technical_debt">technical debts</a> in the Rails code base. This is a really hard job and definitely not a very glamorous one. He&#8217;s been working on various parts of Rails including its router and its ORM (ActiveRecord). Technical debts are unfortunately normal in most project, but sometimes they are overwhelming to the point that nobody dares touching the code base to clean it up. This is a hard problem, especially when projects move fast like Rails did. But looking back, I think that you want to start tackling technical debts on the side as you move on so you avoid getting to the point that you need a hero to come up and clean the piled errors made in the past. But don&#8217;t pause your entire project to clean things up otherwise you will lose market, momentum and excitement. I feel that this is also very much true for any legacy project you might pick up as a developer.</p>

<p> </p>

<h2>Keep the cost of entry level low</h2>

<p>Getting started with Rails used to be easier. This can obviously argued since it&#8217;s very subjective, but from my perspective I think we forgot where we come from and we involuntary expect new comers to come with unrealistic knowledge. Sure, Rails does much more than it used to do, but it&#8217;s also much harder to get started. I&#8217;m not going to argue how harder  it is now or why we got there. Let&#8217;s just keep in mind that it is a critical thing that should always be re-evaluated. Sure, it&#8217;s harder when you have an open source project, but it&#8217;s also up to the leadership to show that they care and to encourage and mentor volunteers to  focus on this important part of a project.</p>

<p> </p>

<h2>Documentation</h2>

<p>Rails documentation isn&#8217;t bad, but it&#8217;s far from being great. Documentation certainly isn&#8217;t one of the Ruby&#8217;s community strength, especially compared with the Python community, but what saddens me is to see the state of <a href="http://guides.rubyonrails.org/">the official documentation</a> which, should, in theory be the reference. Note that the Rails guides are usually well written and provide value, but they too often seem too light and not useful when you try to do something not totally basic (for instance use an ActiveModel compliant object). That&#8217;s probably why most people don&#8217;t refer to them or don&#8217;t spend too much time there. I&#8217;m not trying to blame anyone there. I think that the people who contributed theses guides did an amazing job, but if you want to build a strong and easy to access community, great documentation is key. Look at the <a href="https://docs.djangoproject.com/en/1.3/">Django</a> documentation as a good example. That said, I also need to acknowledge the amazing job done by many community members such as <a href="http://railscasts.com/">Ryan Bates</a> and <a href="http://ruby.railstutorial.org/">Michael Hartl</a> consistently providing high value external documentation via the <a href="http://railscasts.com/">railscasts</a> and the intro to <a href="http://ruby.railstutorial.org/">Rails tutorial</a> available for free.</p>

<p> </p>

<p>In conclusion, I think that there is a lot to learn from Rails, lots of great things as well as lots of things you would want to avoid. We can certainly argue on Hacker News or via comments about whether or not I&#8217;m right about Rails failures, my point will still be that the mentioned issues should be avoided in any projects, Rails here is just an example. Many of these issues are currently being addressed by the Rails team but wouldn&#8217;t it be great if new projects learn from older ones and avoid making the same mistakes? So what other mistakes do you think I forgot to mention and that one should be very careful of avoiding?</p>

<p> </p>

<h3>Updates:</h3>

<ol>
<li><p>Rails 4 had an API centric app generator but it <a href="https://github.com/rails/rails/commit/6db930cb5bbff9ad824590b5844e04768de240b1">was quickly reverted</a> and will live as gem until it&#8217;s mature enough.</p></li>
<li><p>Rails 4 improved the ActiveModel API to be simpler to get started with. See <a href="http://blog.plataformatec.com.br/2012/03/barebone-models-to-use-with-actionpack-in-rails-4-0/">this blog</a> post for more info.</p></li>
</ol>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Quick dive into Ruby ORM object initialization]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/02/23/quick-dive-into-ruby-orm-object-initialization/"/>
    <updated>2012-02-23T09:46:49-08:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/02/23/quick-dive-into-ruby-orm-object-initialization</id>
    <content type="html"><![CDATA[<p>Yesterday I did some quick digging into how ORM objects are initialized and the performance cost associated to that. In other words, I wanted to see what&#8217;s going on when you initialize an ActiveRecord object.</p>

<p>Before I show you the benchmark numbers and you jump to conclusions, it&#8217;s important to realize that in the grand scheme of things, the performance cost we are talking is small enough that it is certainly not the main reason why your application is slow. Spoiler alert: ActiveRecord is slow but the cost of initialization isn&#8217;t by far the worse part of ActiveRecord. Also, even though this article doesn&#8217;t make activeRecord look good, and I&#8217;m not trying to diss it. It&#8217;s a decent ORM that does a great job in most cases.</p>

<p>Let&#8217;s get started by the benchmarks number to give us an idea of the damage (using Ruby 1.9.3 p125):</p>

<p> </p>

<pre><code>                                                             | Class | Hash  | AR 3.2.1 | AR no protection | Datamapper | Sequel |
--------------------------------------------------------------------------------------------------------------------------------------
.new() x100000                                               | 0.037 | 0.049 | 1.557    | 1.536            | 0.027      | 0.209  |
.new({:id=&gt;1, :title=&gt;"Foo", :text=&gt;"Bar"}) x100000          | 0.327 | 0.038 | 6.784    | 5.972            | 4.226      | 1.986  |
</code></pre>

<p> </p>

<p>You can see that I am comparing the allocation of a Class instance, a Hash and some ORM models. The benchmark suite tests the allocation of an empty object and one with passed attributes. The benchmark in question is available <a href="https://github.com/mattetti/benchmarks/blob/master/init_objects.rb">here</a>.</p>

<p>As you can see there seems to be a huge performance difference between allocating a basic class and an ORM class. Instantiating an ActiveRecord class is 20x slower than instantiating a normal class, while ActiveRecord offers some extra features, why is it so much slower, especially at initialization time?</p>

<p>The best way to figure it out is to profile the initialization. For that, I used <a href="https://github.com/tmm1/perftools.rb">perftools.rb</a> and I generated a graph of the call stack.</p>

<p>Here is what Ruby does (and spends its time) when you initialize a new Model instance (click to download the PDF version):</p>

<p> </p>

<p><a href="http://github.com/mattetti/benchmarks/blob/master/ar_init_profile.pdf?raw=true"><img src="http://merbist.com/wp-content/uploads/2012/02/AR-model-instantation-by-Matt-Aimonetti.jpg" alt="Profiler diagram of AR model instantiation by Matt Aimonetti" /></a></p>

<p> </p>

<p>This is quite a scary graph but it shows nicely the features you are getting and their cost associated. For instance, the option of having the before and after initialization callback cost you 14% of your CPU time per instantiation, even though you probably almost never use these callbacks. I&#8217;m reading that by interpreting the node called ActiveSupport::Callback#run_callbacks, 3rd level from the top. So 14.1% of the CPU time is spent trying to run callbacks. As a quick note, note that 90.1% of the CPU time is spent initializing objects, the rest is spent in the loop and in the garbage collection (because the profiler runs many loops). You can then follow the code and see how the code works, creating a dynamic class callback method on the fly (the one with the long name) and then recreating the name of this callback to call it each time the object is allocated. It sounds like that&#8217;s a good place for some micro optimizations which could yield up to 14% performance increase in some cases.</p>

<p>Another major part of the CPU time is spent in ActiveModel&#8217;s sanitization. This is the piece of code that allows you to block some model attributes to be mass assigned. This is useful when you don&#8217;t want to sanitize your incoming params but want to create or update a model instance by using all the passed user params. To avoid malicious users to modify some specific params that might be in your model but not in your form, you can protect these attributes. A good example would be an admin flag on a User object. That said, if you manually initialize an instance, you don&#8217;t need this extra protection, that&#8217;s why in the benchmark above, I tested and without the protection. As you can see, it makes quite a big difference. The profiler graph of the same initialization without the mass assignment protection logically ends up looking quite different:</p>

<p> </p>

<p><a href="https://github.com/mattetti/benchmarks/blob/master/ar_init_no_protection.pdf?raw=true">
</a><a href="https://github.com/mattetti/benchmarks/blob/master/ar_init_no_protection.pdf?raw=true"><img src="http://merbist.com/wp-content/uploads/2012/02/AR-model-instantiation-without-mass-assignment-by-Matt-Aimonetti.jpg" alt="Matt Aimonetti shows the stack trace generated by the instantiation of an Active Record model" /></a></p>

<p> </p>

<p><strong>Update:</strong> My colleague <a href="https://twitter.com/#!/glv">Glenn Vanderburg</a> pointed out that some people might assuming that the shown code path is called for each record loaded from the database. This isn&#8217;t correct, the graph represents instances allocated by calling #new. See the addition at the bottom of the post for more details about what&#8217;s going on when you fetch data from the DB.</p>

<p>I then decided to look at the graphs for the two other popular Ruby ORMs:</p>

<p><a href="http://datamapper.org/">Datamapper</a></p>

<p><a href="https://github.com/mattetti/benchmarks/blob/master/dm_init_profile.pdf?raw=true"><img src="http://img.skitch.com/20120223-txs4wa7b5rdpg45aj6354xg1wt.jpg" alt="" /></a></p>

<p> </p>

<p>and <a href="http://sequel.rubyforge.org/">Sequel</a></p>

<p><a href="https://github.com/mattetti/benchmarks/blob/master/sequel_init_profile.pdf?raw=true"><img src="http://img.skitch.com/20120223-p2jx6ypk35ucsgtx7p1tcabpes.jpg" alt="" /></a></p>

<p> </p>

<p> </p>

<p>While I didn&#8217;t give you much insight in ORM code, I hope that this post will motivate you to sometimes take a look under the cover and profile your code to see what&#8217;s going on and why it might be slow. <strong>Never assume, always measure</strong>. Tools such as perftools are a great way to get a visual feedback and get a better understanding of how the Ruby interpreter is handling your code.</p>

<h2>UPDATE:</h2>

<p>I heard you liked graphs so I added some more, here is what&#8217;s going on when you do Model.first:</p>

<p><a href="https://github.com/mattetti/benchmarks/blob/master/ar_first_profile.pdf?raw=true"><img src="http://img.skitch.com/20120224-f23s8xctghi8mj6ax3cdw9aq25.jpg" alt="" /></a></p>

<p> </p>

<p>Model.all</p>

<p><a href="https://github.com/mattetti/benchmarks/blob/master/ar_all_profile.pdf?raw=true"><img src="https://img.skitch.com/20120224-q29q4n7bj3i96erk1enxdqxb5e.jpg" alt="" /></a></p>

<p> </p>

<p>And finally this is the code graph for a call to Model.instantiate which is called after a record was retrieved from the database to convert into an Object. (You can see the #instantiate call referenced in the graph above).</p>

<p> </p>

<p><a href="https://github.com/mattetti/benchmarks/blob/master/ar_instantiate_profile.pdf?raw=true"><img src="http://img.skitch.com/20120224-8scmun9n1c9ufdnxa8rq2961bq.jpg" alt="" /></a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[LA RubyConf 2012 - Ruby: time to move on]]></title>
    <link href="http://matt.aimonetti.net/posts/2012/02/04/larubyconf-2012/"/>
    <updated>2012-02-04T15:09:00-08:00</updated>
    <id>http://matt.aimonetti.net/posts/2012/02/04/larubyconf-2012</id>
    <content type="html"><![CDATA[<p>During <a href="http://larubyconf.org/">LA RubyConf 2012</a> in Los Angeles, CA Matt
Aimonetti gave a talk entitled <em>Ruby: time to move on</em>.</p>

<h2>Description of the talk:</h2>

<p>Let&#8217;s be honest, Ruby became mainstream a few years back and it isn&#8217;t the cool underground programming language it once was. It&#8217;s quite likely that your cousin&#8217;s boyfriend who&#8217;s &#8220;into computers&#8221; knows what Ruby on Rails is. There are hundreds of books, conferences, training and meetups for Rubyists. Recruiters fight to hire whoever knows how to generate a scaffolded Rails app. But now cool kids can&#8217;t stop talking about node.js, CoffeeScript, Clojure, Haskell and pushing code to the UI layer. What does it mean for the new, existing and prospecting Ruby developers? Is it time to jump ship and move on to something else?</p>

<p><img src="http://matt.aimonetti.net/images/matt_aimonetti_timeToMoveOn.jpg" alt="Matt Aimonetti showing that the end result is what matters the most" /></p>

<h2>Slides</h2>

<script async class="speakerdeck-embed" data-id="4f904c27a542080022020653" data-ratio="1.299492385786802" src="http://speakerdeck.com/assets/embed.js"></script>


<p>The slides are available on <a href="http://speakerdeck.com/u/matt_aimonetti/p/ruby-time-to-move-on">Matt&#8217;s SpeakerDeck</a> and can be <a href="http://matt.aimonetti.net/slides/matt_aimonetti-ruby_time_to_move_on.pdf">downloaded here</a>.</p>

<h2>Video</h2>

<p><video width='640' height='360' preload='none' controls poster=' /images/matt_aimonetti_larubyconf2012_video.png'><source src='http://cdn.confreaks.com/system/assets/datas/3173/original/816-larubyconf2012-time-to-move-away-from-ruby-small.mp4' type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'/></video></p>

<h2>Presentation website</h2>

<p>Matt&#8217;s presentation was filmed by <a href="http://confreaks.com">Confreaks</a> and posted <a href="http://confreaks.com/videos/816-larubyconf2012-time-to-move-away-from-ruby">here</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Books to read in 2012 - recommended to me by Twitter]]></title>
    <link href="http://matt.aimonetti.net/posts/2011/12/30/books-to-read-in-2012-recommended-to-me-by-twitter/"/>
    <updated>2011-12-30T15:28:13-08:00</updated>
    <id>http://matt.aimonetti.net/posts/2011/12/30/books-to-read-in-2012-recommended-to-me-by-twitter</id>
    <content type="html"><![CDATA[<p>Today, I asked on Twitter what non-technical books I should read in 2012.</p>

<p>I was nicely surprised to see so many of my followers send recommendations. Here is a list of 25 books that like-minded people suggested I read. Hopefully you will find a book or two to read too. Feel free to send more recommendations via the comments.</p>

<p> </p>

<p><a href="http://www.amazon.com/gp/product/0307593312/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0307593312"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0307593312&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0307593312" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0307593312/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0307593312">1Q84 by Haruki Murakami</a></p>

<p>suggested by <a href="https://twitter.com/#!/mrb_bk">@mrb_bk</a> and <a href="https://twitter.com/#!/chadfowler">@chadfowler</a></p>

<p><a href="http://www.amazon.com/gp/product/0385240899/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0385240899"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0385240899&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0385240899" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0385240899/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0385240899">The Floating Opera and The End of the Road by John Barth</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0385240899" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/chadfowler">@chadfowler</a></p>

<p><a href="http://www.amazon.com/gp/product/0613663616/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0613663616"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0613663616&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0613663616" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0613663616/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0613663616">Into Thin Air by Jon Krakauer</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0613663616" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/bradly">@bradly</a></p>

<p><a href="http://www.amazon.com/gp/product/0375714367/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0375714367"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0375714367&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0375714367" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0375714367/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0375714367">Cutting for Stone by Abraham Verghese</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0375714367" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/bradly">@bradly</a></p>

<p><a href="http://www.amazon.com/gp/product/0452011876/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0452011876"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0452011876&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0452011876" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0452011876/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0452011876">Atlas Shrugged by Ayn Rand</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0452011876" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/bradly">@bradly</a></p>

<p><a href="http://www.amazon.com/gp/product/0307474720/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0307474720"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0307474720&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0307474720" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0307474720/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0307474720">Cien años de soledad by Gabriel Garcia Marquez</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0307474720" alt="" /> (es)</p>

<p>suggested by <a href="https://twitter.com/#!/romanandreg">@romanandreg</a> &amp; <a href="https://twitter.com/#!/jrfernandez">@jrfernandez</a> &amp; <a href="https://twitter.com/#!/edgarschmidt">@edgarschmidt</a></p>

<p><a href="http://www.amazon.com/gp/product/0060883286/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0060883286"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0060883286&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0060883286" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0060883286/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0060883286">One Hundred Years of Solitude by Gabriel García Marquez</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0060883286" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/romanandreg">@romanandreg</a> &amp; <a href="https://twitter.com/#!/jrfernandez">@jrfernandez</a> &amp; <a href="https://twitter.com/#!/edgarschmidt">@edgarschmidt</a></p>

<p><a href="http://www.amazon.com/gp/product/0553348981/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0553348981"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0553348981&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0553348981" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0553348981/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0553348981">Jitterbug Perfume by Tom Robbins</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0553348981" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/supaspoida">@supaspoida</a></p>

<p><a href="http://www.amazon.com/gp/product/0062041266/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0062041266"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0062041266&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0062041266" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0062041266/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0062041266">The Sisters Brothers by Patrick deWitt</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0062041266" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/dennismajor1">@dennismajor1</a></p>

<p><a href="http://www.amazon.com/gp/product/0312278497/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0312278497"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0312278497&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0312278497" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0312278497/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0312278497">The Glass Bead Game by Hermann Hesse</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0312278497" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/dj2sincl">@dj2sincl</a></p>

<p><a href="http://www.amazon.com/gp/product/0679775439/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0679775439"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0679775439&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0679775439" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0679775439/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0679775439">The Wind-Up Bird Chronicle by Haruki Murakami</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0679775439" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/chadfowler">@chadfowler</a></p>

<p><a href="http://www.amazon.com/gp/product/0983873100/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0983873100"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0983873100&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0983873100" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0983873100/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0983873100">Mindfire by Scott Berkun</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0983873100" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/lucasdicioccio">@lucasdicioccio</a></p>

<p><a href="http://www.amazon.com/gp/product/2226052577/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=2226052577"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=2226052577&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=2226052577" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/2226052577/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=2226052577">Les Fourmis by Bernard Werber</a> (fr)</p>

<p>suggested by <a href="https://twitter.com/#!/twitty_tim">@twitty_tim</a></p>

<p><a href="http://www.amazon.com/gp/product/0375725849/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0375725849"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0375725849&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0375725849" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0375725849/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0375725849">Perfume: The Story of a Murderer by Patrick Suskind</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0375725849" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/twitty_tim">@twitty_tim</a></p>

<p><a href="http://www.amazon.com/gp/product/1613820259/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1613820259"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=1613820259&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=1613820259" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/1613820259/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1613820259">Les Miserables by Victor Hugo</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=1613820259" alt="" /> (en, free ebook)</p>

<p>suggested by <a href="https://twitter.com/#!/tutec">@tutec</a></p>

<p><a href="http://www.amazon.com/gp/product/0307292134/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0307292134"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0307292134&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0307292134" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0307292134/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0307292134">Song Of Ice and Fire by George R.R. Martin</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0307292134" alt="" /> (Game of Thrones saga)</p>

<p>suggested by <a href="https://twitter.com/#!/eeppa">@eeppa</a> &amp; <a href="http://twitter.com/jarin">@jarin</a></p>

<p><a href="http://www.amazon.com/gp/product/0765329468/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0765329468"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0765329468&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0765329468" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0765329468/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0765329468">Clockwork Century by Cherie Priest</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0765329468" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/eeppa">@eeppa</a></p>

<p><a href="http://www.amazon.com/gp/product/1590201183/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1590201183"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=1590201183&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=1590201183" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/1590201183/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1590201183">The Darkness that Comes Before by R. Scott Bakker</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=1590201183" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/eeppa">@eeppa</a></p>

<p><a href="http://www.amazon.com/gp/product/B003GAN3VE/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B003GAN3VE"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=B003GAN3VE&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=B003GAN3VE" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/B003GAN3VE/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B003GAN3VE">Drood by Dan Simmons</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=B003GAN3VE" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/eeppa">@eeppa</a></p>

<p><a href="http://www.amazon.com/gp/product/0316068225/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0316068225"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0316068225&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0316068225" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0316068225/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0316068225">This Is Water by David Foster Wallace</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0316068225" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/atduskgreg">@atduskgreg</a></p>

<p><a href="http://www.amazon.com/gp/product/B005DI71QA/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B005DI71QA"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=B005DI71QA&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=B005DI71QA" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/B005DI71QA/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=B005DI71QA">Anathem by Neal Stephenson</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=B005DI71QA" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/jarin">@jarin</a></p>

<p><a href="http://www.amazon.com/gp/product/0812550706/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0812550706"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0812550706&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0812550706" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0812550706/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0812550706">Ender&#8217;s Game by Orson Scott Card</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0812550706" alt="" /> (entire saga)</p>

<p>suggested by <a href="https://twitter.com/#!/jarin">@jarin</a> &amp; <a href="https://twitter.com/#!/edgarschmidt">@edgarschmidt</a></p>

<p><a href="http://www.amazon.com/gp/product/344245302X/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=344245302X"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=344245302X&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=344245302X" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/344245302X/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=344245302X">Snow Crash by Neal Stephenson</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=344245302X" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/jarin">@jarin</a></p>

<p><a href="http://www.amazon.com/gp/product/1422171647/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1422171647"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=1422171647&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=1422171647" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/1422171647/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1422171647">Fixing the Game by Roger L. Martin</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=1422171647" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/jarkko">@jarkko</a></p>

<p><a href="http://www.amazon.com/gp/product/0307387895/ref=as_li_ss_il?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0307387895"><img src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL110_&amp;ASIN=0307387895&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=merbist-20&amp;ServiceVersion=20070822" alt="" /></a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0307387895" alt="" /></p>

<p><a href="http://www.amazon.com/gp/product/0307387895/ref=as_li_ss_tl?ie=UTF8&amp;tag=merbist-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=0307387895">The Road by Cormac McCarthy</a><img src="http://www.assoc-amazon.com/e/ir?t=merbist-20&amp;l=as2&amp;o=1&amp;a=0307387895" alt="" /></p>

<p>suggested by <a href="https://twitter.com/#!/mrreynolds">@mrreynolds</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Developing a Curriculum]]></title>
    <link href="http://matt.aimonetti.net/posts/2011/12/21/developing-a-curriculum/"/>
    <updated>2011-12-21T06:57:27-08:00</updated>
    <id>http://matt.aimonetti.net/posts/2011/12/21/developing-a-curriculum</id>
    <content type="html"><![CDATA[<p>Recently I asked a friend of mine to give me pointers on how to develop a curriculum (he used to teach an education PHD program), after discussing his response on Twitter, people asked me to put it somewhere, so here it is:</p>

<p>Process to develop a curriculum:</p>

<p><strong>Purpose</strong>. <em>Know why you&#8217;re doing what you&#8217;re doing.</em></p>

<ul>
<li>You know how to do this.</li>
</ul>


<p><strong>Product</strong>. <em>Start with the end in mind.</em></p>

<ul>
<li><p>What does the student look like when they walk out the door at the end of the training.</p></li>
<li><p>Usually, we break these down into <strong>Knowledge</strong>, <strong>Skills</strong>, or <strong>Attitudes</strong>.</p></li>
<li><p>Sometimes it&#8217;s helpful to see a photograph or drawing of a someone who finished the program and just talk about what they can do that makes them successful.</p></li>
<li><p>This &#8220;product&#8221; should be connected and help you accomplish your mission</p></li>
</ul>


<p><strong>Practices</strong>. <em>Then ask yourself, &#8220;How do people become like this?&#8221;</em></p>

<ul>
<li><p>If you can break down your Product into 3-5 bit-sized chunks, then see how people learn each one of those skills, gain each one of those knowledge points, and how to they gain the attitudes you want them to have.</p></li>
<li><p>This one is much easier the more experience you have in seeing people develop the &#8220;Product.&#8221;</p></li>
<li><p>This is also easier to determine when you understand <a href="http://en.wikipedia.org/wiki/Learning_theory_(education">Learning Theory</a>).</p></li>
<li><p>The results from this section will result in a list of:</p>

<ul>
<li><p>       Activities or experiences</p></li>
<li><p>       Resources. What books, website, teachers, software, etc. will help them learn more effectively and efficiently</p></li>
<li><p>       Assessments. How you would know if the activity was helpful?</p></li>
</ul>
</li>
</ul>


<p><strong>Plans</strong>. <em>Make your plans based on the practices you&#8217;ve determined you&#8217;ve needed.</em></p>

<p> </p>

<p>On a related topic, Chad Fowler posted an interesting <a href="http://chadfowler.com/2011/12/21/re-thinking-software-development-education">blog post about what LivingSocial is doing to change the software development education</a>.</p>
]]></content>
  </entry>
  
</feed>
