<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Skorks</title>
    <link>https://skorks.com/</link>
    <description>Recent content on Skorks</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Sat, 16 Aug 2008 16:14:55 +0000</lastBuildDate>
    
        <atom:link href="https://skorks.com/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Ruby &amp; (Ampersand) Parameter Demystified</title>
      <link>https://skorks.com/2013/04/ruby-ampersand-parameter-demystified/</link>
      <pubDate>Tue, 16 Apr 2013 14:28:07 +0000</pubDate>
      
      <guid>https://skorks.com/2013/04/ruby-ampersand-parameter-demystified/</guid>
      
        <description>


&lt;link rel=&#34;stylesheet&#34; href=&#34;https://skorks.com/css/hugo-easy-gallery.css&#34; /&gt;
&lt;div class=&#34;box&#34; &gt;
  &lt;figure  itemprop=&#34;associatedMedia&#34; itemscope itemtype=&#34;http://schema.org/ImageObject&#34;&gt;
    &lt;div class=&#34;img&#34;&gt;
      &lt;img itemprop=&#34;thumbnail&#34; src=&#34;https://skorks.com/images/2013-04-17-ruby-ampersand-parameter-demystified/idea.jpg#center&#34; alt=&#34;Idea&#34;/&gt;
    &lt;/div&gt;
    &lt;a href=&#34;https://skorks.com/images/2013-04-17-ruby-ampersand-parameter-demystified/idea.jpg#center&#34; itemprop=&#34;contentUrl&#34;&gt;&lt;/a&gt;
  &lt;/figure&gt;
&lt;/div&gt;


&lt;p&gt;Recently I was asked a question about &amp;lsquo;&amp;amp; parameters&amp;rsquo; when you define and/or call methods which take a block e.g.:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;blah&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;yadda&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;yadda&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;foo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;foo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;blah&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;As you pass this parameter around, sometimes the ampersand appears in front of it, but other times it doesn&amp;rsquo;t, seemingly with no rhyme of reason. As we dig into &lt;a href=&#34;https://skorks.com/2013/03/a-closure-is-not-always-a-closure-in-ruby/&#34;&gt;crazy metaprogramming&lt;/a&gt; or write &lt;a href=&#34;https://github.com/skorks/escort&#34;&gt;various libraries&lt;/a&gt;, &lt;strong&gt;it is often hard to remember how confusing Ruby can be when you&amp;rsquo;re starting out&lt;/strong&gt;. So, let&amp;rsquo;s dig into this a little more deeply and shed some light on what&amp;rsquo;s going on.&lt;/p&gt;

&lt;h2 id=&#34;theimplicitblock&#34;&gt;The Implicit Block&lt;/h2&gt;

&lt;p&gt;Methods in Ruby can &lt;a href=&#34;https://skorks.com/2009/08/method-arguments-in-ruby/&#34;&gt;take arguments&lt;/a&gt; in all sorts of interesting ways. One case that&amp;rsquo;s especially interesting is when a Ruby method takes a block.&lt;/p&gt;

&lt;p&gt;In fact, all &lt;strong&gt;Ruby methods can implicitly take a block, without needing to specify this in the parameter list&lt;/strong&gt; or having to use the block within the method body e.g.:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This will execute without any trouble but nothing will be printed out as we&amp;rsquo;re not executing the block that we&amp;rsquo;re passing in. We can &amp;#8211; of course &amp;#8211; easily execute the block by &lt;code&gt;yielding&lt;/code&gt; to it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;yield&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;block_given?&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This time we get some output:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;hello&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We &lt;code&gt;yielded&lt;/code&gt; to the block inside the method, but the fact that the method takes a block is still implicit.&lt;/p&gt;

&lt;p&gt;It gets even more interesting since &lt;strong&gt;Ruby allows to pass any object to a method and have the method attempt to use this object as its block&lt;/strong&gt;. If we put an ampersand in front of the last parameter to a method, Ruby will try to treat this parameter as the method&amp;rsquo;s block. If the parameter is &lt;a href=&#34;https://skorks.com/2010/05/ruby-procs-and-lambdas-and-the-difference-between-them/&#34;&gt;already a &lt;code&gt;Proc&lt;/code&gt; object&lt;/a&gt;, Ruby will simply associate it with the method as its block.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;yield&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;block_given?&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;blah&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;lambda&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;blah&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;lambda&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;If the parameter is not a &lt;code&gt;Proc&lt;/code&gt;, &lt;strong&gt;Ruby will try to convert it into one (&lt;em&gt;by calling &lt;code&gt;to_proc&lt;/code&gt; on it&lt;/em&gt;) before associating it with the method as its block&lt;/strong&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;yield&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;block_given?&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;FooBar&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;to_proc&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;-&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;converted lambda&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;FooBar&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;converted lambda&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;All of this seems pretty clear, but what if I want to take a block that was associated with a method and pass it to another method? We need a way to refer to our block.&lt;/p&gt;

&lt;h2 id=&#34;theexplicitblock&#34;&gt;The Explicit Block&lt;/h2&gt;

&lt;p&gt;When we write our method definition, we can explicitly state that we expect this method to possibly take a block. Confusingly, Ruby uses the ampersand for this as well:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;yield&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;block_given?&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Defining our method this way, gives us a name by which we can refer to our block within the method body. And since our block is a &lt;code&gt;Proc&lt;/code&gt; object, instead of &lt;code&gt;yielding&lt;/code&gt; to it, we can &lt;code&gt;call&lt;/code&gt; it:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;block_given?&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;I prefer &lt;code&gt;block.call&lt;/code&gt; instead of &lt;code&gt;yield&lt;/code&gt;, it makes things clearer&lt;/strong&gt;. Of course, when we define our method we don&amp;rsquo;t have to use the name &amp;lsquo;block&amp;rsquo;, we can do:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;foo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;foo&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;block_given?&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Having said that; &amp;lsquo;block&amp;rsquo; is a good convention.&lt;/p&gt;

&lt;p&gt;So, &lt;strong&gt;in the context of methods and blocks, there are two ways we use the ampersand&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;in the context of a method &lt;strong&gt;definition&lt;/strong&gt;, putting an ampersand in front of the last parameter indicates that a method may take a block and gives us a name to refer to this block within the method body&lt;/li&gt;
&lt;li&gt;in the context of a method &lt;strong&gt;call&lt;/strong&gt;, putting an ampersand in front of the last argument tells Ruby to convert this argument to a &lt;code&gt;Proc&lt;/code&gt; if necessary and then use the object as the method&amp;rsquo;s block&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&#34;passingtwoblockstoamethod&#34;&gt;Passing Two Blocks To A Method&lt;/h3&gt;

&lt;p&gt;It is instructive to see what happens when you try to pass a both a regular block and a block argument to a method:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;block_given?&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;blah&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;lambda&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;blah&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You get the following error message:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;code.rb:56: both block arg and actual block given&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It is not even an exception &amp;#8211; it&amp;rsquo;s a syntax error!&lt;/p&gt;

&lt;h3 id=&#34;usinganothermethodasablock&#34;&gt;Using Another Method As A Block&lt;/h3&gt;

&lt;p&gt;It&amp;rsquo;s also interesting to note that since you can easily get a reference to a method in ruby and the &lt;code&gt;Method&lt;/code&gt; object implements &lt;code&gt;to_proc&lt;/code&gt;, &lt;strong&gt;you can easily give one method as a block to another&lt;/strong&gt; e.g.:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;block_given?&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;world&lt;/span&gt;
  &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;world&amp;#34;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;method_reference&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;method&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;ss&#34;&gt;:world&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method_reference&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;world&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;passingtheblockaround&#34;&gt;Passing The Block Around&lt;/h2&gt;

&lt;p&gt;We now know enough to easily understand our first example:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;blah&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;yadda&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;yadda&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;foo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;foo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;blah&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;we define &lt;code&gt;blah&lt;/code&gt; to expect a block, inside &lt;code&gt;blah&lt;/code&gt; we can refer to this block as &lt;code&gt;block&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;we define &lt;code&gt;yadda&lt;/code&gt; to expect one parameter, this parameter would be referred to as &lt;code&gt;block&lt;/code&gt; inside &lt;code&gt;yadda&lt;/code&gt;, but it is not a &lt;strong&gt;block&lt;/strong&gt; in that we could not &lt;code&gt;yield&lt;/code&gt; to it inside &lt;code&gt;yadda&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;foo&lt;/code&gt; also expects a block and we can refer to this block as &lt;code&gt;block&lt;/code&gt; inside &lt;code&gt;foo&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;when we call &lt;code&gt;yadda&lt;/code&gt; from within &lt;code&gt;blah&lt;/code&gt; we pass it our &lt;code&gt;block&lt;/code&gt; variable without the ampersand, since &lt;code&gt;yadda&lt;/code&gt; does not a expect a block parameter, but simply a regular method argument, in our case this regular method argument will just happen to be a &lt;code&gt;Proc&lt;/code&gt; object&lt;/li&gt;
&lt;li&gt;when we call &lt;code&gt;foo&lt;/code&gt; from inside &lt;code&gt;yadda&lt;/code&gt; we pass it our &lt;code&gt;block&lt;/code&gt; variable, but this time with an ampersand since &lt;code&gt;foo&lt;/code&gt; actually expects a block and we want to give it a block rather than just a regular variable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It should now be much more obvious why the ampersand is used in some cases, but not in others.&lt;/p&gt;

&lt;h2 id=&#34;thesymboltoproctrick&#34;&gt;The Symbol To Proc Trick&lt;/h2&gt;

&lt;p&gt;We should now also have a lot less trouble understanding the &amp;lsquo;symbol to proc&amp;rsquo; trick. You&amp;rsquo;ve no doubt seen code like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;a&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;b&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;].&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;map&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;ss&#34;&gt;:upcase&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We know that this is equivalent to:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;a&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;b&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;].&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;map&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;|&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;upcase&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;But now we also make an educated guess as to why they are equivalent. We have a &lt;code&gt;Symbol&lt;/code&gt; object (&amp;rsquo;:upcase&amp;rsquo;), we put an ampersand in front of it and pass it to the &lt;code&gt;map&lt;/code&gt; method. The &lt;code&gt;map&lt;/code&gt; method takes a block, and by using the ampersand we&amp;rsquo;ve told Ruby that we want to convert our &lt;code&gt;Symbol&lt;/code&gt; object to a &lt;code&gt;Proc&lt;/code&gt; and associate it with the &lt;code&gt;map&lt;/code&gt; method as its block. It turns out that &lt;code&gt;Symbol&lt;/code&gt; implements &lt;code&gt;to_proc&lt;/code&gt; &lt;a href=&#34;http://iain.nl/going-crazy-with-to_proc&#34;&gt;in a special way&lt;/a&gt;, so that the resulting block becomes functionally equivalent to our second example above. Of course &lt;strong&gt;these days Ruby implements &lt;code&gt;Symbol#to_proc&lt;/code&gt; using C&lt;/strong&gt;, so it&amp;rsquo;s not quite as nice looking as the examples you&amp;rsquo;ll &lt;a href=&#34;http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html&#34;&gt;find around the web&lt;/a&gt;, but you get general idea.&lt;/p&gt;

&lt;p&gt;Anyway, hopefully this makes blocks and ampersands a bit more friendly. It&amp;rsquo;s definitely worth it to occasionally revisit the basics, I&amp;rsquo;ll try to do it more often in the future.&lt;/p&gt;

&lt;p&gt;&lt;span class=&#34;image-attribution&#34;&gt;Image by &lt;a href=&#34;http://www.flickr.com/photos/rustychainsaw/4267237788/&#34; target=&#34;_blank&#34;&gt;Martin Whitmore&lt;/a&gt; &lt;/span&gt;&lt;/p&gt;
</description>
      
    </item>
    
    <item>
      <title>Ruby – Why U No Have Nested Exceptions?</title>
      <link>https://skorks.com/2013/04/ruby-why-u-no-have-nested-exceptions/</link>
      <pubDate>Tue, 09 Apr 2013 13:53:21 +0000</pubDate>
      
      <guid>https://skorks.com/2013/04/ruby-why-u-no-have-nested-exceptions/</guid>
      
        <description>

&lt;p&gt;&lt;img src=&#34;http://www.skorks.com/wp-content/uploads/2013/04/why-you-no.jpg&#34; alt=&#34;Why U No&#34; width=&#34;320&#34; height=&#34;240&#34; class=&#34;aligncenter size-full wp-image-2286&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2013/04/why-you-no.jpg 320w, https://www.skorks.com/wp-content/uploads/2013/04/why-you-no-300x225.jpg 300w&#34; sizes=&#34;(max-width: 320px) 100vw, 320px&#34; /&gt;&lt;/p&gt;

&lt;p&gt;One of the things we almost always do these days when we write our libraries and apps, is use other libraries. Inevitably something will go wrong with those libraries and &lt;a href=&#34;https://skorks.com/2009/09/ruby-exceptions-and-exception-handling/&#34;&gt;exceptions&lt;/a&gt; will be produced. Sometimes these are expected (&lt;em&gt;e.g. an HTTP client that produces an exception when you encounter a 500 response or a connection timeout&lt;/em&gt;), sometimes they are unexpected. Either way &lt;strong&gt;you don&amp;rsquo;t want to allow the exceptions from these external libraries to bubble up through your code&lt;/strong&gt; and potentially crash your application or cause other weirdness. Especially considering that many of these exceptions will be custom types from the libraries you&amp;rsquo;re using. No-one wants strange exceptions percolating through their code.&lt;/p&gt;

&lt;p&gt;What you want to do, is ensure that all interactions with these external libraries are wrapped in a &lt;code&gt;begin..rescue..end&lt;/code&gt;. You catch all external errors and can now decide how to handle them. You can throw your hands up in the air and just re-raise the same error:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
  &lt;span class=&#34;no&#34;&gt;SomeExternalLibrary&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;do_stuff&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;raise&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This doesn&amp;rsquo;t really win us anything. Better yet &lt;strong&gt;you would raise one of your own custom error types&lt;/strong&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
  &lt;span class=&#34;no&#34;&gt;SomeExternalLibrary&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;do_stuff&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;raise&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;MyNamespace&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;MyError&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This way you know that once you&amp;rsquo;re past your interfaces with the external libraries you can only encounter exception types that you know about.&lt;/p&gt;

&lt;h2 id=&#34;theneedfornestedexceptions&#34;&gt;The Need For Nested Exceptions&lt;/h2&gt;

&lt;p&gt;The problem is that by raising a custom error, we lose all the information that was contained in the original error that we rescued. This information would have potentially been of great value to help us diagnose/debug the problem (&lt;em&gt;that caused the error in the first place&lt;/em&gt;), but it is lost with no way to get it back. In this regard it would have been better to re-raise the original error. &lt;strong&gt;What we want is to have the best of both worlds, raise a custom exception type, but retain the information from the original exception&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;When writing &lt;a href=&#34;https://github.com/skorks/escort&#34;&gt;escort&lt;/a&gt; one of the things I wanted was informative errors and stack traces. I wanted to raise errors and add information (&lt;em&gt;by rescuing and re-raising&lt;/em&gt;) as they percolated through the code, to be handled in one place. What I needed was the ability to nest exceptions within other exceptions.&lt;/p&gt;

&lt;p&gt;Ruby doesn&amp;rsquo;t allow us to nest exceptions. However, I remembered &lt;a href=&#34;http://about.avdi.org/&#34;&gt;Avdi Grimm&lt;/a&gt; mentioning the &lt;a href=&#34;http://nestegg.rubyforge.org/&#34;&gt;nestegg gem&lt;/a&gt; in his excellent &lt;a href=&#34;http://exceptionalruby.com/&#34;&gt;Exceptional Ruby&lt;/a&gt; book, so I decided to give it a try.&lt;/p&gt;

&lt;h2 id=&#34;theproblemswithnestegg&#34;&gt;The Problems With Nestegg&lt;/h2&gt;

&lt;p&gt;&lt;img src=&#34;http://www.skorks.com/wp-content/uploads/2013/04/egg.jpg&#34; alt=&#34;Egg&#34; width=&#34;228&#34; height=&#34;320&#34; class=&#34;aligncenter size-full wp-image-2285&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2013/04/egg.jpg 228w, https://www.skorks.com/wp-content/uploads/2013/04/egg-213x300.jpg 213w&#34; sizes=&#34;(max-width: 228px) 100vw, 228px&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Unfortunately &lt;code&gt;nestegg&lt;/code&gt; is a bit old and a little buggy:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It would sometimes lose the error messages&lt;/li&gt;
&lt;li&gt;Nesting more than one level deep would cause repetition in the stacktrace&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I also didn&amp;rsquo;t like how it made the stack trace look non-standard when including the information from the nested errors. If we take some code similar to the following:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;nb&#34;&gt;require&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;nestegg&amp;#39;&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MyError&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;lt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;StandardError&lt;/span&gt;
  &lt;span class=&#34;kp&#34;&gt;include&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;Nestegg&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;NestingException&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
  &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;/&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;raise&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;MyError&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Number errors will be caught&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;raise&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;MyError&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Don&amp;#39;t need to let MyError bubble up&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;raise&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;MyError&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Last one for sure!&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It would produce a stack trace like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;examples/test1.rb:26:in `rescue in rescue in rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;: MyError (MyError)
	from examples/test1.rb:23:in `rescue in rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from examples/test1.rb:20:in `rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from examples/test1.rb:17:in `&amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from cause: MyError: MyError
	from examples/test1.rb:24:in `rescue in rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from examples/test1.rb:20:in `rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from examples/test1.rb:17:in `&amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from cause: MyError: MyError
	from examples/test1.rb:21:in `rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from examples/test1.rb:17:in `&amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from cause: ZeroDivisionError: divided by 0
	from examples/test1.rb:18:in `/&amp;#39;
	from examples/test1.rb:18:in `&amp;amp;lt;main&amp;amp;gt;&amp;#39;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;After looking around I found &lt;a href=&#34;https://github.com/loganb/nestegg&#34;&gt;loganb-nestegg&lt;/a&gt;. This fixed some of the bugs, but still had the non-standard stack trace and the repetition issue.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;When you&amp;rsquo;re forced to look for the 3rd library to solve a problem, it&amp;rsquo;s time to write your own.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is exactly what I did for &lt;a href=&#34;https://github.com/skorks/escort&#34;&gt;escort&lt;/a&gt;. &lt;strong&gt;This functionality eventually got extracted into a gem which is how we got &lt;a href=&#34;https://github.com/skorks/nesty&#34;&gt;nesty&lt;/a&gt;&lt;/strong&gt;. Its stack traces look a lot like regular ones, it doesn&amp;rsquo;t lose messages and you can nest exceptions as deep as you like without ugly repetition in the stack trace. If we take the same code as above, but redefine the error to use &lt;code&gt;nesty&lt;/code&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;MyError&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;lt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;StandardError&lt;/span&gt;
  &lt;span class=&#34;kp&#34;&gt;include&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;Nesty&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;NestedError&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Our stack trace will now be:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;examples/complex.rb:20:in `rescue in rescue in rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;: Last one for sure! (MyError)
	from examples/complex.rb:17:in `rescue in rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from examples/complex.rb:18:in `rescue in rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;: Don&amp;#39;t need to let MyError bubble up
	from examples/complex.rb:14:in `rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from examples/complex.rb:15:in `rescue in &amp;amp;lt;main&amp;amp;gt;&amp;#39;: Number errors will be caught
	from examples/complex.rb:11:in `&amp;amp;lt;main&amp;amp;gt;&amp;#39;
	from examples/complex.rb:12:in `/&amp;#39;: divided by 0
	from examples/complex.rb:12:in `&amp;amp;lt;main&amp;amp;gt;&amp;#39;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Definitely nicer. We simply add the messages for every nested error to the stack trace in the appropriate place (rather than giving them their own line).&lt;/p&gt;

&lt;h2 id=&#34;hownestedexceptionswork&#34;&gt;How Nested Exceptions Work&lt;/h2&gt;

&lt;p&gt;The code for &lt;code&gt;nesty&lt;/code&gt; is tiny, but there are a couple of interesting bits in it worth looking at.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One of the special variables in Ruby is &lt;code&gt;$!&lt;/code&gt; which always contains the last exception that was raised&lt;/strong&gt;. This way when we raise a &lt;code&gt;nesty&lt;/code&gt; error type, we don&amp;rsquo;t have to supply the nested error as a parameter, it will just be looked up in &lt;code&gt;$!&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Ruby always allows you to set a custom backtrace on any error&lt;/strong&gt;. So, if you rescue an error you can always replace its stack trace with whatever you want e.g.:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
  &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;/&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;gt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;message&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;foobar&amp;#34;&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;set_backtrace&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;hello&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;world&amp;#39;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;raise&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;e&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This produces:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;hello: divided by 0 (ZeroDivisionError)
	from world&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We take advantage of this and override the &lt;code&gt;set_backtrace&lt;/code&gt; method to take into account the stack trace of the nested error.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;9
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;set_backtrace&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;backtrace&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;vi&#34;&gt;@raw_backtrace&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;backtrace&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;nested&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;backtrace&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;backtrace&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;nested_raw_backtrace&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;backtrace&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+=&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;#{&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;nested&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;backtrace&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;first&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;: &lt;/span&gt;&lt;span class=&#34;si&#34;&gt;#{&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;nested&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;message&lt;/span&gt;&lt;span class=&#34;si&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;backtrace&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;nested&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;backtrace&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;..-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;||&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[]&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;super&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;backtrace&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;To produce the augmented stack trace we note that the stack trace of the nested error should always be mostly a subset of the enclosing error. So, we whittle down the enclosing stack trace by taking the difference between it and the nested stack trace (&lt;em&gt;I think set operations are really undervalued in Ruby, maybe a good subject for a future post&lt;/em&gt;). We then augment the nested stack trace with the error message and concatenate it with what was left over from the enclosing stack trace.&lt;/p&gt;

&lt;p&gt;Anyway, if you don&amp;rsquo;t want exceptions from other libraries invading your app, but still want the ability to diagnose the cause of the exceptions easily &amp;#8211; &lt;strong&gt;nested exceptions might be the way to go&lt;/strong&gt;. And if you do decide that nested exceptions are a good fit, &lt;a href=&#34;https://github.com/skorks/nesty/&#34;&gt;nesty&lt;/a&gt; is there for you.&lt;/p&gt;

&lt;p&gt;&lt;span class=&#34;image-attribution&#34;&gt;Image by &lt;a href=&#34;http://www.flickr.com/photos/39747297@N05/5230326264/&#34; target=&#34;_blank&#34;&gt;Samuel M. Livingston&lt;/a&gt; &lt;/span&gt;&lt;/p&gt;
</description>
      
    </item>
    
    <item>
      <title>The Best Way To Pretty Print JSON On The Command-Line</title>
      <link>https://skorks.com/2013/04/the-best-way-to-pretty-print-json-on-the-command-line/</link>
      <pubDate>Wed, 03 Apr 2013 07:53:23 +0000</pubDate>
      
      <guid>https://skorks.com/2013/04/the-best-way-to-pretty-print-json-on-the-command-line/</guid>
      
        <description>

&lt;p&gt;&lt;img src=&#34;http://www.skorks.com/wp-content/uploads/2013/04/print.jpg&#34; alt=&#34;Print&#34; width=&#34;320&#34; height=&#34;203&#34; class=&#34;aligncenter size-full wp-image-2276&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2013/04/print.jpg 320w, https://www.skorks.com/wp-content/uploads/2013/04/print-300x190.jpg 300w&#34; sizes=&#34;(max-width: 320px) 100vw, 320px&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Developers tend to work with APIs a lot and these days most of these APIs are JSON. These JSON strings aren&amp;rsquo;t exactly easy to read unless they are formatted well. There are many services online that can pretty print JSON for you, but that&amp;rsquo;s annoying. I love the command-line and whenever I am playing with new APIs or writing my own I mostly use &lt;a href=&#34;http://curl.haxx.se/&#34;&gt;CURL&lt;/a&gt; which means &lt;strong&gt;I need a good way to pretty print my JSON on the command-line&lt;/strong&gt;. It should be simple, quick, easy to remember, easy to install &amp;#8211; we&amp;rsquo;re not trying to solve complex algorithms, just format some JSON.&lt;/p&gt;

&lt;h3 id=&#34;thegoodoldfaithful&#34;&gt;The &amp;lsquo;Good&amp;rsquo; Old Faithful&lt;/h3&gt;

&lt;p&gt;One way that is always available is the &lt;strong&gt;Python JSON tool&lt;/strong&gt;. So you can always do this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;echo &amp;#39;{&amp;#34;b&amp;#34;:2, &amp;#34;a&amp;#34;:1}&amp;#39; | python -mjson.tool&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Which will give you:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;{
    &amp;#34;a&amp;#34;: 1,
    &amp;#34;b&amp;#34;: 2
}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This is alright and, as I said, it is always available. However note that &lt;strong&gt;it has sorted our keys which is a major disadvantage&lt;/strong&gt;. It is also a bit of a handful to write when you just want to pretty print some JSON. I only ever use this when I am on an unfamiliar computer and there is nothing better.&lt;/p&gt;

&lt;h3 id=&#34;yajltools&#34;&gt;YAJL Tools&lt;/h3&gt;

&lt;p&gt;If you&amp;rsquo;re not using &lt;a href=&#34;http://lloyd.github.com/yajl/&#34;&gt;YAJL&lt;/a&gt; you should be. It is a small JSON library written in C. The parser is event driven and super fast. In the Ruby world we have &lt;a href=&#34;https://github.com/brianmario/yajl-ruby&#34;&gt;a nice set of bindings&lt;/a&gt; and there are bindings for other languages as well. It is my go-to JSON library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;YAJL also cames with a couple of tools, &lt;code&gt;json_reformat&lt;/code&gt; and &lt;code&gt;json_verify&lt;/code&gt;&lt;/strong&gt;. These are pretty self-explanatory and you can get your hands on them like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;brew install yajl&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;sudo apt-get install yajl-tools&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;After that all you have to do is:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;echo &amp;#39;{&amp;#34;b&amp;#34;:2, &amp;#34;a&amp;#34;:1}&amp;#39; | json_reformat&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Which will give you:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;{
    &amp;#34;b&amp;#34;: 2,
    &amp;#34;a&amp;#34;: 1
}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This is pretty nice. My one tiny niggle is that &lt;code&gt;json_reformat&lt;/code&gt; is still a bit of a mouthful, but if you just want basic JSON formatting, it&amp;rsquo;s a good solution.&lt;/p&gt;

&lt;h3 id=&#34;ppjson&#34;&gt;Ppjson&lt;/h3&gt;

&lt;p&gt;Of course being developers, we don&amp;rsquo;t have to put up with even minor niggles since we can build ourselves exactly the tools we need (and a little bit extra to boot). I was writing a &lt;a href=&#34;https://github.com/skorks/escort&#34;&gt;command-line framework&lt;/a&gt; and &lt;strong&gt;I just happened to need a command-line tool, which is how I ended up with &lt;a href=&#34;https://github.com/skorks/ppjson&#34;&gt;ppjson&lt;/a&gt;&lt;/strong&gt;. I use Ruby quite a bit, so it is a Ruby tool and even if I do say so myself, it&amp;rsquo;s pretty nice.&lt;/p&gt;

&lt;p&gt;You just need to:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;gem install ppjson&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This will let you do:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;echo &amp;#39;{&amp;#34;b&amp;#34;:2, &amp;#34;a&amp;#34;:1}&amp;#39; | ppjson&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Which will give you:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;{
  &amp;#34;b&amp;#34;: 2,
  &amp;#34;a&amp;#34;: 1
}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It uses YAJL through &lt;a href=&#34;https://github.com/intridea/multi_json&#34;&gt;multi_json&lt;/a&gt; under the hood, so you still get the speed of YAJL, but it can also do a few extra things for you.&lt;/p&gt;

&lt;p&gt;You can pass or pipe it some JSON and it will pretty print it to standard output for you. But you can also pass it a file name with some JSON in it and it will pretty print the contents of the file for you:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;ppjson -f abc123.json&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;You can also store the pretty printed contents back into the file:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;ppjson -fi abc123.json&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Sometimes you have saved some pretty printed JSON in a file, but now you want to use it as a body of a CURL POST request, for example. Well &lt;strong&gt;&lt;code&gt;ppjson&lt;/code&gt; can uglify your JSON for you as well&lt;/strong&gt;:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;ppjson -fu abc123.json&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This will output a minified JSON string to standard output. And of course you can also update the original file with the uglified JSON as well:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;ppjson -fui abc123.json&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It will do you basic JSON pretty printing with an easy to remember executable name, but it also has a few nice convenience features to make your life that little bit easier.&lt;/p&gt;

&lt;p&gt;The best part is that using Escort](&lt;a href=&#34;https://github.com/skorks/escort&#34;&gt;https://github.com/skorks/escort&lt;/a&gt;), it was a breeze to write. I&amp;rsquo;ll [talk about some of the other interesting projects that &lt;em&gt;&amp;lsquo;fell out&amp;rsquo;&lt;/em&gt; of Escort some other time.&lt;/p&gt;

&lt;p&gt;Anyway, now you no longer need to remember that IDE or editor shortcut to format your JSON or look for an online tool to do the same, the command-line has you more than covered.&lt;/p&gt;

&lt;p&gt;&lt;span class=&#34;image-attribution&#34;&gt;Image by &lt;a href=&#34;http://www.flickr.com/photos/62693815@N03/6277208708/&#34; target=&#34;_blank&#34;&gt;NS Newsflash&lt;/a&gt; &lt;/span&gt;&lt;/p&gt;
</description>
      
    </item>
    
    <item>
      <title>I Was Nominated To Speak At LoneStarRuby Conference 2013</title>
      <link>https://skorks.com/2013/04/i-was-nominated-to-speak-at-lonestarruby-conference-2013/</link>
      <pubDate>Mon, 01 Apr 2013 07:38:06 +0000</pubDate>
      
      <guid>https://skorks.com/2013/04/i-was-nominated-to-speak-at-lonestarruby-conference-2013/</guid>
      
        <description>&lt;p&gt;A few days ago I woke up to see this in my Twitter stream:&lt;/p&gt;

&lt;blockquote class=&#34;twitter-tweet&#34; align=&#34;center&#34;&gt;
  &lt;p&gt;
    @&lt;a href=&#34;https://twitter.com/indianguru&#34;&gt;indianguru&lt;/a&gt; Your recommendation for @&lt;a href=&#34;https://twitter.com/skorks&#34;&gt;skorks&lt;/a&gt; to speak at @&lt;a href=&#34;https://twitter.com/lsrc&#34;&gt;lsrc&lt;/a&gt; is now public. Go to &lt;a href=&#34;http://t.co/H4SwcVkraw&#34; title=&#34;http://portal.lonestarruby.org&#34;&gt;portal.lonestarruby.org&lt;/a&gt; to vote!
  &lt;/p&gt;

  &lt;p&gt;
    &amp;mdash; Lance Vaughn (@lance_io) &lt;a href=&#34;https://twitter.com/lance_io/status/317749620124037122&#34;&gt;March 29, 2013&lt;/a&gt;
  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Looking at the list of &lt;a href=&#34;http://portal.lonestarruby.org/&#34;&gt;nominated speakers&lt;/a&gt;, I am in extremely illustrious company which was just amazing to see. Of course I replied with my appreciation. Then a little later I saw this:&lt;/p&gt;

&lt;blockquote class=&#34;twitter-tweet&#34; data-conversation=&#34;none&#34; align=&#34;center&#34;&gt;
  &lt;p&gt;
    @&lt;a href=&#34;https://twitter.com/skorks&#34;&gt;skorks&lt;/a&gt; @&lt;a href=&#34;https://twitter.com/lance_io&#34;&gt;lance_io&lt;/a&gt; @&lt;a href=&#34;https://twitter.com/indianguru&#34;&gt;indianguru&lt;/a&gt; @&lt;a href=&#34;https://twitter.com/lsrc&#34;&gt;lsrc&lt;/a&gt; I&#39;d leave SF for atx for @&lt;a href=&#34;https://twitter.com/skorks&#34;&gt;skorks&lt;/a&gt; &amp;#8230; And @&lt;a href=&#34;https://twitter.com/torchytaco&#34;&gt;torchytaco&lt;/a&gt;
  &lt;/p&gt;

  &lt;p&gt;
    &amp;mdash; Steven G. Harms (@sgharms) &lt;a href=&#34;https://twitter.com/sgharms/status/317850488261455872&#34;&gt;March 30, 2013&lt;/a&gt;
  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Which was also very nice.&lt;/p&gt;

&lt;p&gt;Who would have thought something like this would ever happen &amp;#8211; thanks &lt;a href=&#34;https://twitter.com/IndianGuru&#34;&gt;@IndianGuru&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Anyway, if &lt;a href=&#34;http://www.lonestarruby.org/2013/lsrc&#34;&gt;LoneStarRuby Conference 2013&lt;/a&gt; is of any interest to you and you&amp;rsquo;d like to &lt;del&gt;make me write a talk&lt;/del&gt; &lt;ins&gt;hear me speak&lt;/ins&gt;, you can &lt;a href=&#34;http://portal.lonestarruby.org/&#34;&gt;go and vote&lt;/a&gt;.&lt;/p&gt;
</description>
      
    </item>
    
    <item>
      <title>A Closure Is Not Always A Closure In Ruby</title>
      <link>https://skorks.com/2013/03/a-closure-is-not-always-a-closure-in-ruby/</link>
      <pubDate>Wed, 27 Mar 2013 05:53:18 +0000</pubDate>
      
      <guid>https://skorks.com/2013/03/a-closure-is-not-always-a-closure-in-ruby/</guid>
      
        <description>

&lt;p&gt;&lt;img src=&#34;http://www.skorks.com/wp-content/uploads/2013/03/surprise.jpg&#34; alt=&#34;Surprise&#34; width=&#34;350&#34; height=&#34;331&#34; class=&#34;aligncenter size-full wp-image-2129&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2013/03/surprise.jpg 350w, https://www.skorks.com/wp-content/uploads/2013/03/surprise-300x283.jpg 300w&#34; sizes=&#34;(max-width: 350px) 100vw, 350px&#34; /&gt;Often you don&amp;rsquo;t really think about what particular language features mean, until those features come and bite you. For me one such feature was Ruby&amp;rsquo;s &lt;a href=&#34;http://ruby-doc.org/core-2.0/BasicObject.html#method-i-instance_eval&#34;&gt;&lt;code&gt;instance_eval&lt;/code&gt;&lt;/a&gt;. We all know how &lt;code&gt;instance_eval&lt;/code&gt; works, it&amp;rsquo;s pretty straight forward. We pass it a block, and that block gets executed in the context of the object on which we call &lt;code&gt;instance_eval&lt;/code&gt; e.g.:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;n&#34;&gt;a&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;Object&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance_eval&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;hello&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;a&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;hello&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The object &lt;code&gt;a&lt;/code&gt; will get a &lt;code&gt;hello&lt;/code&gt; method and the subsequent &lt;code&gt;a.hello&lt;/code&gt; call will succeed. So what&amp;rsquo;s the gotcha?&lt;/p&gt;

&lt;p&gt;The one overwhelming use of &lt;code&gt;instance_eval&lt;/code&gt; is for creating configuration DSLs. I won&amp;rsquo;t go into that right now &amp;#8211; it deserves a post of its own. Suffice to say that when used for this purpose, the &lt;a href=&#34;https://skorks.com/2010/05/closures-a-simple-explanation-using-ruby/&#34;&gt;closure semantics&lt;/a&gt; of the block that you pass to &lt;code&gt;instance_eval&lt;/code&gt; don&amp;rsquo;t matter, i.e. you don&amp;rsquo;t tend to use the variables (or call methods) from the scope where the block was created. &lt;strong&gt;If you do need to use the variables or methods from the scope where the block was created, you might be in for a rude surprise&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&#34;whatitmeanstoevaluateablockinadifferentcontext&#34;&gt;What It Means To Evaluate A Block In A Different Context&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s create a class with a method that takes a block and call that method from within another class:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;A&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;method1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;B&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;initialize&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;instance_var1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;A&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;method2&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;local1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method1&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;s2&#34;&gt;&amp;#34;helper1&amp;#34;&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;B&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;As expected we get:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&amp;#34;local1&amp;#34;
&amp;#34;instance_var1&amp;#34;
&amp;#34;helper1&amp;#34;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What happens is that when we create the block inside &lt;code&gt;method2&lt;/code&gt;, there is a binding that goes along with it. Inside the binding there is a &lt;code&gt;self&lt;/code&gt; object which is our instance &lt;code&gt;b&lt;/code&gt; of the &lt;code&gt;B&lt;/code&gt; class. &lt;strong&gt;When we execute the block inside &lt;code&gt;method1&lt;/code&gt;, it is executed within the context of this &lt;code&gt;self&lt;/code&gt; and so our local variable, instance variable and helper method are all in scope (just as you would expect from a closure)&lt;/strong&gt; and everything works fine. Let&amp;rsquo;s modify &lt;code&gt;method1&lt;/code&gt; slightly to get the &lt;code&gt;self&lt;/code&gt; from the binding:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;A&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;method1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;puts&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;binding&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eval&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;self&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;object_id&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;B&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;object_id&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We get:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;70229187307420
70229187307420
&amp;#34;local1&amp;#34;
&amp;#34;instance_var1&amp;#34;
&amp;#34;helper1&amp;#34;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;So both &lt;code&gt;b&lt;/code&gt; and the &lt;code&gt;self&lt;/code&gt; inside the binding are the same object as we expected.&lt;/p&gt;

&lt;p&gt;But, what happens if instead of calling &lt;code&gt;method1&lt;/code&gt; with a block on &lt;code&gt;@a&lt;/code&gt; we &lt;code&gt;instance_eval&lt;/code&gt; the same block within the context of &lt;code&gt;@a&lt;/code&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;A&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;B&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;initialize&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;instance_var1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;A&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;method2&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;local1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance_eval&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;s2&#34;&gt;&amp;#34;helper1&amp;#34;&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;B&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We get:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&amp;#34;local1&amp;#34;
nil
code2.rb:19:in `block in method2&amp;#39;: undefined local variable or method `helper1&amp;#39; for #&amp;amp;lt;A:0x007f966292d308&amp;amp;gt; (NameError)&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This time &lt;code&gt;local1&lt;/code&gt; is fine, the instance variable is &lt;code&gt;nil&lt;/code&gt; and trying to call the method raises an error. This is unexpected, &lt;strong&gt;the block should be a closure, but only the local variables were closed over&lt;/strong&gt;, everything else fell apart. This is &lt;a href=&#34;http://olabini.com/blog/2008/09/dont-overuse-instance_eval-and-instance_exec/&#34;&gt;not a new issue&lt;/a&gt;, but it is important to understand what is happening and why.&lt;/p&gt;

&lt;p&gt;When we call &lt;code&gt;instance_eval&lt;/code&gt; on an object, the &lt;code&gt;self&lt;/code&gt; within the block is set to be the object on which we called &lt;code&gt;instance_eval&lt;/code&gt; (Yahuda has &lt;a href=&#34;http://yehudakatz.com/2009/11/15/metaprogramming-in-ruby-its-all-about-the-self/&#34;&gt;more detail&lt;/a&gt; and there is &lt;a href=&#34;http://yugui.jp/articles/846&#34;&gt;even more detail here&lt;/a&gt;). So, even though we still manage to capture the locals from the previous scope, the methods (like &lt;code&gt;helper1&lt;/code&gt;) are no longer in scope within our new &lt;code&gt;self&lt;/code&gt; and the instance variables will be equal to &lt;code&gt;nil&lt;/code&gt; since we haven&amp;rsquo;t initialized them in this new scope (unless you happen to have an instance variable with the same name in which case it will shadow the one from the scope in which the block was defined &amp;#8211; which is probably not what you want).&lt;/p&gt;

&lt;p&gt;So, even though we know that blocks in Ruby are closures, there is an exception to every rule.&lt;/p&gt;

&lt;h2 id=&#34;howtoovercomethelimitations&#34;&gt;How To Overcome The Limitations&lt;/h2&gt;

&lt;p&gt;So what can we do if we want access to the instance variables and helper method from the scope where the block was defined. Well, for instance variables, &lt;strong&gt;one way around would be to pass them in as parameters to the &lt;code&gt;instance_eval&lt;/code&gt; block&lt;/strong&gt;, that way they could be treated as locals. Unfortunately &lt;code&gt;instance_eval&lt;/code&gt; doesn&amp;rsquo;t take parameters. Luckily some time around Ruby 1.8.7 &lt;a href=&#34;http://ruby-doc.org/core-2.0/BasicObject.html#method-i-instance_exec&#34;&gt;&lt;code&gt;instance_exec&lt;/code&gt;&lt;/a&gt; was introduced. Since &lt;code&gt;instance_eval&lt;/code&gt; is so much more popular we sometimes forget that &lt;code&gt;instance_exec&lt;/code&gt; is even there, it essentially does the same thing as &lt;code&gt;instance_eval&lt;/code&gt;, but you can pass some arguments to the block.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;B&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;initialize&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;instance_var1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;A&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;method2&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;local1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance_exec&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance_var1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;|&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;instance_var1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;s2&#34;&gt;&amp;#34;helper1&amp;#34;&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;B&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We get:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&amp;#34;local1&amp;#34;
&amp;#34;instance_var1&amp;#34;
code2.rb:15:in `block in method2&amp;#39;: undefined local variable or method `helper1&amp;#39; for #&amp;amp;lt;A:0x007fea7912d3e0&amp;amp;gt; (NameError)&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This time our instance variable is fine since we&amp;rsquo;ve turned it into an argument, but we still can&amp;rsquo;t call the method. Still not nice, but &lt;strong&gt;&lt;code&gt;instance_exec&lt;/code&gt; is obviously somewhat more useful than &lt;code&gt;instance_eval&lt;/code&gt;&lt;/strong&gt;. We will come back to handling method calls shortly, but first a bit of history.&lt;/p&gt;

&lt;h2 id=&#34;sowhatdidtheydobeforeinstance_execexisted&#34;&gt;So What Did They Do Before &lt;code&gt;instance_exec&lt;/code&gt; Existed&lt;/h2&gt;

&lt;p&gt;&lt;img src=&#34;http://www.skorks.com/wp-content/uploads/2013/03/old.jpg&#34; alt=&#34;Old&#34; width=&#34;400&#34; height=&#34;266&#34; class=&#34;aligncenter size-full wp-image-2131&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2013/03/old.jpg 400w, https://www.skorks.com/wp-content/uploads/2013/03/old-300x199.jpg 300w&#34; sizes=&#34;(max-width: 400px) 100vw, 400px&#34; /&gt;&lt;/p&gt;

&lt;p&gt;This issue has been around for ages, but &lt;code&gt;instance_exec&lt;/code&gt; has only been around for a few years, so what did they do before that, when they wanted to pass parameters to &lt;code&gt;instnace_eval&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;When I decided to write &lt;a href=&#34;https://github.com/skorks/escort&#34;&gt;Escort&lt;/a&gt;, I chose &lt;a href=&#34;http://trollop.rubyforge.org/&#34;&gt;Trollop&lt;/a&gt; as the option parser. It is while reading the Trollop source that I accidentally stumbled upon the answer to the above question. &lt;strong&gt;The author of Trollop attributes it to &lt;a href=&#34;http://en.wikipedia.org/wiki/Why_the_lucky_stiff&#34;&gt;_why&lt;/a&gt;. It&amp;rsquo;s called the &lt;code&gt;cloaker&lt;/code&gt;&lt;/strong&gt; and we use it as a replacement for &lt;code&gt;instance_eval&lt;/code&gt;. Here is how it works:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;A&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;cloaker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;lt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;lt&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;class_eval&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;define_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;instance_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;remove_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;B&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;initialize&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;instance_var1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;A&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;method2&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;local1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;cloaker&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance_var1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;|&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;instance_var1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bind&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;s2&#34;&gt;&amp;#34;helper1&amp;#34;&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;B&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We get:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&amp;#34;local1&amp;#34;
&amp;#34;instance_var1&amp;#34;
code2.rb:24:in `block in method2&amp;#39;: undefined local variable or method `helper1&amp;#39; for #&amp;amp;lt;A:0x007fb963064900&amp;amp;gt; (NameError)&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;It is somewhat more awkward to use, but the output is identical to that of &lt;code&gt;instance_exec&lt;/code&gt;. So why does this work?&lt;/p&gt;

&lt;p&gt;We define a method on the metaclass of &lt;code&gt;A&lt;/code&gt; using our block as the body, we then remove it and return the &lt;code&gt;UnboundMethod&lt;/code&gt;. We then bind the unbound method to our target object and call the method.&lt;/p&gt;

&lt;p&gt;In order for the bind to work, the relationship between the object to which we are binding and the object on which the &lt;code&gt;UnboundMethod&lt;/code&gt; was originally defined must be a &lt;code&gt;kind_of? == true&lt;/code&gt;. &lt;strong&gt;Curiously an instance of the class &lt;code&gt;A&lt;/code&gt; is a &lt;code&gt;kind_of?&lt;/code&gt; &lt;code&gt;metaclass of A&lt;/code&gt; which is why everything works&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&#34;themanyfacesofcloaker&#34;&gt;The Many Faces Of Cloaker&lt;/h2&gt;

&lt;p&gt;Why did we have to define our &lt;code&gt;UnboundMethod&lt;/code&gt; on the metaclass of &lt;code&gt;A&lt;/code&gt;? Well there doesn&amp;rsquo;t seem to be any good reason really. A cloaker like this will work just fine.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;A&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;cloaker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;class&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance_eval&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;define_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;instance_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;remove_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;So will this one:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;A&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;cloaker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;nb&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;class&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;class_eval&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;define_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;instance_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;remove_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;kind_of?&lt;/code&gt; relationship will hold in both cases so everything will hang together.&lt;/p&gt;

&lt;p&gt;Did we really need to go through all that history, we just want to call helper methods from within the scope where our block is defined?&lt;/p&gt;

&lt;h2 id=&#34;whattodoaboutmethodcalls&#34;&gt;What To Do About Method Calls&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s have a look at how we can get our helper methods back. &lt;strong&gt;One obvious way is to assign &lt;code&gt;self&lt;/code&gt; to a local&lt;/strong&gt; and then use this local to call our helper methods from within the &lt;code&gt;instance_exec&lt;/code&gt; block.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;A&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;B&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;initialize&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;instance_var1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;A&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;method2&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;local1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;local_self&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;self&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance_exec&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance_var1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;|&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;instance_var1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;local_self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;s2&#34;&gt;&amp;#34;helper1&amp;#34;&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;B&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We get:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&amp;#34;local1&amp;#34;
&amp;#34;instance_var1&amp;#34;
&amp;#34;helper1&amp;#34;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Everything works, but it is ugly and if you want to call a private helper method, you&amp;rsquo;re out of luck again since a private method &lt;a href=&#34;https://skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/&#34;&gt;can&amp;rsquo;t have an explicit receiver&lt;/a&gt;. This is where our cloaker comes into its own again. &lt;strong&gt;Using a cloaker and a bit of &lt;a href=&#34;http://www.dan-manges.com/blog/ruby-dsls-instance-eval-with-delegation&#34;&gt;&lt;code&gt;method_missing&lt;/code&gt; magic&lt;/a&gt; we can create our own version of &lt;code&gt;instance_exec&lt;/code&gt;&lt;/strong&gt; that has all the semantics we&amp;rsquo;ve come to expect from a closure, but will still execute the block within the context of the object on which it is called. We can also abstract away the ugly &lt;code&gt;bind&lt;/code&gt; stuff while we&amp;rsquo;re at it.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;34
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;37
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;38
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;39
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;40
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;41
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;42
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;43
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;44
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;45
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;46
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;47
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;48
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;A&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;cloaker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;args&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;class&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;class_eval&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;define_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;instance_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;remove_method&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:cloaker_&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;with_previous_context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;binding&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;meth&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bind&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;args&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)}&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;with_previous_context&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;binding&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@previous_context&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;binding&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;eval&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;self&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;result&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;call&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@previous_context&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kp&#34;&gt;nil&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;result&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;method_missing&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;method&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;args&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;vi&#34;&gt;@previous_context&lt;/span&gt;
      &lt;span class=&#34;vi&#34;&gt;@previous_context&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;send&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nb&#34;&gt;method&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;*&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;args&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;k&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;B&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;initialize&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;instance_var1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;A&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;method2&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;local1&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;vi&#34;&gt;@a&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;cloaker&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;vi&#34;&gt;@instance_var1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;|&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;instance_var1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;|&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;local1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;instance_var1&lt;/span&gt;
      &lt;span class=&#34;nb&#34;&gt;p&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

  &lt;span class=&#34;kp&#34;&gt;private&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;helper1&lt;/span&gt;
    &lt;span class=&#34;s2&#34;&gt;&amp;#34;helper1&amp;#34;&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;

&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;B&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;new&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method2&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Despite the fact that &lt;code&gt;helper1&lt;/code&gt; was private, we get:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&amp;#34;local1&amp;#34;
&amp;#34;instance_var1&amp;#34;
&amp;#34;helper1&amp;#34;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Essentially when we execute the block within the context of on object; we retain (in an instance variable) the &lt;code&gt;self&lt;/code&gt; from the context where the block was defined (we get this &lt;code&gt;self&lt;/code&gt; from the biding of the block). When a method is called within the block and it is not defined within the current context &amp;#8211; &lt;code&gt;method_missing&lt;/code&gt; will be called as per usual Ruby semantics. &lt;strong&gt;We hook into &lt;code&gt;method_missing&lt;/code&gt; and send the method call to the &lt;code&gt;self&lt;/code&gt; that we have retained from when the block was defined&lt;/strong&gt;. This will call our method or fail if it was a genuine error.&lt;/p&gt;

&lt;p&gt;Despite the fact that Ruby can surprise you with non-closure blocks, you can have your cake and eat it too, if you&amp;rsquo;re willing to do just a bit of work and give some new legs to an old trick while you&amp;rsquo;re at it.&lt;/p&gt;

&lt;p&gt;&lt;span class=&#34;image-attribution&#34;&gt;Images by &lt;a href=&#34;http://www.flickr.com/photos/ittybittiesforyou/4969087042/&#34; target=&#34;_blank&#34;&gt;Jenn and Tony Bot&lt;/a&gt; and &lt;a href=&#34;http://www.flickr.com/photos/tarynmarie/218965633/&#34; target=&#34;_blank&#34;&gt;TarynMarie&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
      
    </item>
    
    <item>
      <title>What Is A Startup (Or A Startup Idea)?</title>
      <link>https://skorks.com/2011/11/what-is-a-startup-or-a-startup-idea/</link>
      <pubDate>Wed, 23 Nov 2011 00:18:05 +0000</pubDate>
      
      <guid>https://skorks.com/2011/11/what-is-a-startup-or-a-startup-idea/</guid>
      
        <description>&lt;p&gt;&lt;img align=&#34;left&#34; alt=&#34;Pirate&#34; class=&#34;alignleft size-medium wp-image-2001&#34; height=&#34;199&#34; hspace=&#34;25&#34; src=&#34;http://www.skorks.com/wp-content/uploads/2011/11/pirate-300x199.jpg&#34; title=&#34;Pirate&#34; vspace=&#34;5&#34; width=&#34;300&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2011/11/pirate-300x199.jpg 300w, https://www.skorks.com/wp-content/uploads/2011/11/pirate.jpg 500w&#34; sizes=&#34;(max-width: 300px) 100vw, 300px&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Is a startup all about the idea? Is it a bunch of people hacking together? It is not quite a company in the traditional sense of the word (&lt;em&gt;at least not yet&lt;/em&gt;). I like to think of a startup as a series of assumptions. Your startup idea is just one assumption or alternatively there are many assumptions inherent in your idea. Let&amp;rsquo;s take a simple one we&amp;rsquo;re all familiar with:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;&amp;ldquo;People want to connect with their friends online&amp;rdquo;&lt;/strong&gt;&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;The main assumption is that people actually DO want to connect with their friends online (&lt;em&gt;of course we know now that they do, but a few years ago it wasn&amp;rsquo;t so obvious&lt;/em&gt;). Some of the inherent assumptions might be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;People want to know what their friends are doing right now&lt;/li&gt;
&lt;li&gt;People want to see interesting links from their friends&lt;/li&gt;
&lt;li&gt;We have a way to acquire users in the initial stages&lt;/li&gt;
&lt;li&gt;People will want to let their friends know about our service&lt;/li&gt;
&lt;li&gt;People will want to pay for our service&lt;/li&gt;
&lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your startup is really just a means to validate all those assumptions. If you can do it, then you may pronounce your startup a success (&lt;em&gt;it&amp;rsquo;s not necessarily a success yet, but the rest is really just optimisation&lt;/em&gt;), if you can&amp;rsquo;t then it is a failure. Of course things are a bit more complex than that.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;If you follow the &lt;a href=&#34;http://theleanstartup.com/book&#34; target=&#34;_blank&#34;&gt;Lean Startup movement&lt;/a&gt;&amp;nbsp;at all, you&amp;rsquo;re probably aware of &lt;a href=&#34;http://market-by-numbers.com/tag/pirate-metrics/&#34; target=&#34;_blank&#34;&gt;pirate metrics&lt;/a&gt;&amp;nbsp;(&lt;em&gt;the pirate says &amp;lsquo;AARRR&amp;rsquo;&lt;/em&gt;). We have Acquisition, Activation, Retention, Referral, Revenue. &lt;strong&gt;The thing about those metrics is that they are a funnel&lt;/strong&gt; and they are all about users. We start with lots of users in the Acquisition stage and we end up with less users in the Revenue stage.&amp;nbsp;&lt;/p&gt;

&lt;p style=&#34;text-align: center; &#34;&gt;
  &lt;a href=&#34;http://www.skorks.com/wp-content/uploads/2011/11/aarrr.png&#34;&gt;&lt;img align=&#34;middle&#34; alt=&#34;AARRR&#34; class=&#34;aligncenter size-medium wp-image-2003&#34; height=&#34;250&#34; src=&#34;http://www.skorks.com/wp-content/uploads/2011/11/aarrr-265x300.png&#34; title=&#34;AARRR&#34; vspace=&#34;5&#34; width=&#34;221&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2011/11/aarrr-265x300.png 265w, https://www.skorks.com/wp-content/uploads/2011/11/aarrr.png 303w&#34; sizes=&#34;(max-width: 221px) 100vw, 221px&#34; /&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;All the assumptions inherent in your idea are a funnel too&lt;/strong&gt; (&lt;em&gt;think of them as a more specific version of the pirate funnel&lt;/em&gt;) &amp;#8211; you start with a bunch of potential users at the top and validating each of the assumptions will whittle away some of those users. Let&amp;rsquo;s take this simplified example:&lt;/p&gt;

&lt;p style=&#34;text-align: center; &#34;&gt;
  &lt;img align=&#34;middle&#34; alt=&#34;Assumption funnel&#34; class=&#34;aligncenter size-medium wp-image-2004&#34; height=&#34;250&#34; src=&#34;http://www.skorks.com/wp-content/uploads/2011/11/funnel-300x229.png&#34; title=&#34;Assumption funnel&#34; vspace=&#34;5&#34; width=&#34;328&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2011/11/funnel-300x229.png 300w, https://www.skorks.com/wp-content/uploads/2011/11/funnel.png 756w&#34; sizes=&#34;(max-width: 328px) 100vw, 328px&#34; /&gt;
&lt;/p&gt;

&lt;p&gt;The more people you can get through that funnel successfully the more sure you can be that you&amp;rsquo;re onto a good thing, but if you end up with zero users before you get to the end of your funnel then you have a problem. If no-one wants to click the red button, then people are unlikely to ever tell their friends or upgrade their accounts. Your assumption is flawed and you need to adjust. Perhaps it&amp;rsquo;s as simple as making the button green, or you may have to think of a way to achieve the same result without a button, or there might be a fundamental flaw in the model, in which case you might need to go back to square one, or pivot, or abandon the idea all together.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Of course your assumption funnel is not strict, &lt;strong&gt;you can work on any assumption at any time, but you need to be fully aware of what you&amp;rsquo;re doing&lt;/strong&gt;. Firstly, it is easier to test the assumptions at the top of the funnel. You can figure out whether or not you can drive some initial traffic with nothing more than a dummy landing page, but to test retention you need to have built something. Secondly, there is &amp;lsquo;&lt;em&gt;Danger, Will Robinson!&lt;/em&gt;&amp;rsquo; every time you work on one of the assumptions lower down in the funnel first. You may optimise your big red button with 3 of your mates (&lt;em&gt;the only users you have&lt;/em&gt;), but if you can never get anyone to come to the site at all, what&amp;rsquo;s the use? It can still all work out, especially if you&amp;rsquo;re flush with money and have all the time in the world, but it&amp;rsquo;s probably faster and easier to prove those assumptions in order.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;Incidentally this is very similar to the problem we ran into with &lt;a href=&#34;https://www.crowdhired.com/&#34; target=&#34;_blank&#34;&gt;CrowdHired&lt;/a&gt;&amp;nbsp;forcing us to essentially rebuild the site (&lt;em&gt;in a much slimmed down fashion&lt;/em&gt;) and to try and bootstrap (&lt;em&gt;userbase-wise&lt;/em&gt;) from the &lt;a href=&#34;http://yowaustralia.com.au/&#34; target=&#34;_blank&#34;&gt;YOW! developer conference&lt;/a&gt;. Since we&amp;rsquo;re not flush with time and money it was a costly lesson, but one well learned. &amp;nbsp;&lt;/p&gt;

&lt;p&gt;The relevant concept is this, even without building anything you can easily figure out the key actions you and your potential users will have to take in order for your idea to work. You can then come up with a set of assumptions relating to each of those actions and arrange them into a funnel. &lt;strong&gt;You can then build the minimal amount of stuff to try and prove each assumption in turn&lt;/strong&gt;. As you follow this process it can potentially give you some guidance regarding a number of things, such as user behaviour, where to take your idea next, when to pivot, when to get some more funding (&lt;em&gt;e.g. the only way to prove this next assumption is with 10 people working for a year :)&lt;/em&gt;) etc.&amp;nbsp;&lt;/p&gt;

&lt;p&gt;It goes without saying that depending on the type of startup your trying to build (&lt;em&gt;network effect, B2B, B2C, etc. &amp;#8211; see what I did there, heh&lt;/em&gt;), the mechanics will need adjustment. It also goes without saying that you need to validate all assumptions properly, asking 2 of your friends if they would click a button you just drew on a napkin is not an indicator of anything. In-fact this is one of the things I&amp;rsquo;d like to explore further, but I guess I&amp;rsquo;ll leave it until next time.&lt;/p&gt;

&lt;p&gt;&lt;span style=&#34;font-size: 10px; font-family: trebuchet ms&#34;&gt;Image by &lt;a href=&#34;http://www.flickr.com/photos/spaceninja/1407558031/&#34; target=&#34;_blank&#34;&gt;spaceninja&lt;/a&gt; &lt;/span&gt;&lt;/p&gt;
</description>
      
    </item>
    
    <item>
      <title>Why Developers Never Use State Machines</title>
      <link>https://skorks.com/2011/09/why-developers-never-use-state-machines/</link>
      <pubDate>Thu, 01 Sep 2011 00:38:47 +0000</pubDate>
      
      <guid>https://skorks.com/2011/09/why-developers-never-use-state-machines/</guid>
      
        <description>

&lt;p&gt;A few months ago I saw a great little blog post about &lt;a href=&#34;http://www.shopify.com/technology/3383012-why-developers-should-be-force-fed-state-machines&#34; target=&#34;_blank&#34;&gt;state machines on the Shopify blog&lt;/a&gt;. The message was that state machines are great and developers should use them more &amp;#8211; given my recent experiences with state machines at &lt;a href=&#34;https://www.crowdhired.com/&#34; target=&#34;_blank&#34;&gt;CrowdHired&lt;/a&gt;, I could certainly agree with that. But it got me thinking, how many times in my developer career have I actually used a state machine (&lt;em&gt;either separate library or even hand-rolled abstraction&lt;/em&gt;)? The answer is zero times &amp;#8211; which surprised the hell out of me since state machines really are very useful. So I decided to engage in a bit of introspection and figure out why we tend to manage our &amp;ldquo;&lt;em&gt;state&lt;/em&gt;&amp;rdquo; and &amp;ldquo;&lt;em&gt;status&lt;/em&gt;&amp;rdquo; fields in an ad-hoc fashion rather than doing what is clearly called for.&lt;/p&gt;

&lt;h2 id=&#34;we-don-t-need-one-until-we-do&#34;&gt;We Don&amp;rsquo;t Need One Until We Do&lt;/h2&gt;

&lt;p&gt;The problem is that &lt;strong&gt;you almost never create an object fully formed with all the behaviour it is ever going to need&lt;/strong&gt;, rather you build it up over time. The same is true for the &amp;ldquo;&lt;em&gt;states&lt;/em&gt;&amp;rdquo; that a state machine candidate object can be in. So, early on you don&amp;rsquo;t feel like your objects&amp;rsquo; state machine behaviour is complex enough to warrant a &amp;ldquo;&lt;em&gt;full-blown&lt;/em&gt;&amp;rdquo; state machine (_&lt;a href=&#34;http://www.skorks.com/2009/08/does-yagni-mean-you-ignore-the-obvious/&#34; target=&#34;_blank&#34;&gt;YAGNI&lt;/a&gt;&amp;nbsp;and all that jazz&lt;em&gt;), but later on &amp;#8211; when it IS complex enough &amp;#8211; you feel like you&amp;rsquo;ve invested too much time/effort to replace it with something that has equivalent functionality. It&amp;rsquo;s a bit of a &amp;lt;a href=&amp;ldquo;&lt;a href=&#34;http://en.wikipedia.org/wiki/Catch-22&#34;&gt;http://en.wikipedia.org/wiki/Catch-22&lt;/a&gt;&lt;/em&gt;(logic)&amp;rdquo; target=&amp;rdquo;_blank&amp;rdquo;&amp;gt;catch-22&lt;/a&gt;. &lt;strong&gt;It&amp;rsquo;s overkill and by the time it&amp;rsquo;s not, it&amp;rsquo;s too late&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&#34;a-state-machine-is-a-fluffy-bunny-not-particularly-threatening&#34;&gt;A State Machine Is A Fluffy Bunny (&lt;em&gt;Not Particularly Threatening&lt;/em&gt;)&lt;/h2&gt;

&lt;p style=&#34;text-align: center; &#34;&gt;
  &lt;img align=&#34;middle&#34; alt=&#34;Bunny&#34; class=&#34;aligncenter size-medium wp-image-1992&#34; height=&#34;240&#34; src=&#34;http://www.skorks.com/wp-content/uploads/2011/09/bunny-300x240.jpg&#34; title=&#34;Bunny&#34; vspace=&#34;5&#34; width=&#34;300&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2011/09/bunny-300x240.jpg 300w, https://www.skorks.com/wp-content/uploads/2011/09/bunny.jpg 500w&#34; sizes=&#34;(max-width: 300px) 100vw, 300px&#34; /&gt;
&lt;/p&gt;

&lt;p&gt;Those of us who went through computer science degrees remember state machines from our computing theory subjects and the memories are often not fond ones. There are complex diagrams and math notation, determinism and non-determinism, &lt;a href=&#34;http://en.wikipedia.org/wiki/Finite-state_machine#Transducers&#34; target=&#34;_blank&#34;&gt;Moore and Mealy&lt;/a&gt;, as well as acronyms galore (&lt;em&gt;DFA, NFA, GNFA etc.&lt;/em&gt;). We come to believe that state machines are more complex than they actually are and it is therefore nothing but pragmatism that makes us consider a &amp;ldquo;&lt;em&gt;full-blown&lt;/em&gt;&amp;rdquo; state machine overkill.&lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;most state machines you&amp;rsquo;re likely to need in your day-to-day development have nothing in common with their computing theory counterparts&lt;/strong&gt; (&lt;em&gt;except the &amp;#8230; errr &amp;#8230; theory&lt;/em&gt;). You have states which are strings, and events which are methods that cause transitions from one state to another &amp;#8211; that&amp;rsquo;s pretty much it (&lt;em&gt;at least for the &lt;code&gt;state_machine&lt;/code&gt; gem in Ruby&lt;/em&gt;). The point is, even if you have two states, a state machine is not overkill, it might be easier that rolling an ad-hoc solution, as long as you have a good library to lean on.&lt;/p&gt;

&lt;h2 id=&#34;even-a-good-tool-is-not-a-good-tool&#34;&gt;Even A Good Tool Is Not A Good Tool&lt;/h2&gt;

&lt;p&gt;I would hazard a guess that there are decent state machine libraries for most languages that you can use (&lt;em&gt;the aforementioned &lt;code&gt;state_machine&lt;/code&gt; for Ruby is just one example&lt;/em&gt;). But even a fluffy bunny has a learning curve (&lt;em&gt;I am stretching the metaphor well past breaking point here&lt;/em&gt;). That wouldn&amp;rsquo;t be such an issue if you were solving a problem, but all you&amp;rsquo;re likely doing is replacing an existing solution. Since we tend to turn to a state machine library after the fact (&lt;em&gt;our ad-hoc solution is working right now&lt;/em&gt;). Just &lt;strong&gt;like with everything that has &amp;ldquo;&lt;em&gt;potential future benefits&lt;/em&gt;&amp;rdquo; the immediate value is very hard to justify even to yourself&lt;/strong&gt; (&lt;em&gt;unless you&amp;rsquo;ve had experience with it before&lt;/em&gt;). The slight learning curve only tips the scale further towards the &amp;ldquo;&lt;em&gt;we can live without it&lt;/em&gt;&amp;rdquo; side. It doesn&amp;rsquo;t matter how good a tool is if you never give it a chance.&lt;/p&gt;

&lt;p&gt;It is really difficult to appreciate (&lt;em&gt;until you&amp;rsquo;ve gone through it&lt;/em&gt;) &amp;#8211; how much better life can be if you do give a good state machine library a chance. When we finally &amp;ldquo;&lt;em&gt;bit the bullet&lt;/em&gt;&amp;rdquo; at &lt;a href=&#34;https://www.crowdhired.com/&#34; target=&#34;_blank&#34;&gt;CrowdHired&lt;/a&gt;&amp;nbsp;and rejigged some of our core objects to use the &lt;code&gt;state_machine&lt;/code&gt; gem, the difference was immediately apparent.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Firstly the learning curve was minor, I did spend a few hours of going through the source and documentation, but after that I had a good idea what could and couldn&amp;rsquo;t be done (&lt;em&gt;I might do an in-depth look at the &lt;code&gt;state_machine&lt;/code&gt; gem at some point&lt;/em&gt;).&amp;nbsp;&lt;/li&gt;
&lt;li&gt;The integration itself was almost painless, but moving all the code around to be inline with the new state machine was a big pain. In hindsight &lt;strong&gt;had we done this when our objects only had a couple of states it would have been a breeze&lt;/strong&gt;.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;We&amp;rsquo;re now able to easily introduce more states to give our users extra information as well as allow us to track things to a finer grain. Before it was YAGNI cause it was a pain, now we find that we &amp;ldquo;&lt;em&gt;ai gonna need&lt;/em&gt;&amp;rdquo; after all, cause it&amp;rsquo;s so easy.&lt;/li&gt;
&lt;li&gt;Our return values from state transitions are now 100% consistent (&lt;code&gt;true/false&lt;/code&gt;). Before we were returning objects, arrays of objects, nil, true/false depending on who was writing it and when.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;We&amp;rsquo;re now able to keep an audit trail of our state transitions simply by dropping in &lt;a href=&#34;https://github.com/wvanbergen/state_machine-audit_trail&#34; target=&#34;_blank&#34;&gt;state&lt;em&gt;machine-audit_trail&lt;/a&gt;&amp;nbsp;(_see that Shopify post&lt;/em&gt;), before it was too hard to hook it in everywhere so we had nothing.&lt;/li&gt;
&lt;li&gt;We removed a bunch of code and improved our codebase &amp;#8211; always worthy goals as far as I am concerned.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My gut-feel is that most people who read &lt;a href=&#34;http://www.shopify.com/technology/3383012-why-developers-should-be-force-fed-state-machines&#34; target=&#34;_blank&#34;&gt;that Shopify post&lt;/a&gt; agreed with it in spirit, but did nothing about it (t&lt;em&gt;hat&amp;rsquo;s kinda how it was with me&lt;/em&gt;). &lt;strong&gt;We seem to shy away from state machines due to misunderstanding of their complexity and/or an inability to quantify the benefits.&lt;/strong&gt; But, there is less complexity than you would think and more benefits than you would expect as long you don&amp;rsquo;t try to retrofit a state machine after the fact. So next time you have an object that even hints at having a &amp;ldquo;&lt;em&gt;status&lt;/em&gt;&amp;rdquo; field, just chuck a state machine in there, you&amp;rsquo;ll be glad you did. I guarantee it or your money back :).&lt;/p&gt;

&lt;p&gt;&lt;span style=&#34;font-size: 10px; font-family: trebuchet ms&#34;&gt;Image by &lt;a href=&#34;http://www.flickr.com/photos/tfangel/3478405761/&#34; target=&#34;_blank&#34;&gt;tfangel&lt;/a&gt; &lt;/span&gt;&lt;/p&gt;
</description>
      
    </item>
    
    <item>
      <title>When Developers Go To Great Length To Save Typing 4 Letters</title>
      <link>https://skorks.com/2011/08/when-developers-go-to-great-length-to-save-typing-4-letters/</link>
      <pubDate>Wed, 24 Aug 2011 07:43:19 +0000</pubDate>
      
      <guid>https://skorks.com/2011/08/when-developers-go-to-great-length-to-save-typing-4-letters/</guid>
      
        <description>

&lt;p&gt;&lt;img align=&#34;left&#34; alt=&#34;Great Lengths&#34; class=&#34;alignleft size-medium wp-image-1987&#34; height=&#34;200&#34; hspace=&#34;20&#34; src=&#34;http://www.skorks.com/wp-content/uploads/2011/08/great_lengths-300x225.jpg&#34; title=&#34;Great Lengths&#34; vspace=&#34;5&#34; width=&#34;267&#34; srcset=&#34;https://www.skorks.com/wp-content/uploads/2011/08/great_lengths-300x225.jpg 300w, https://www.skorks.com/wp-content/uploads/2011/08/great_lengths.jpg 500w&#34; sizes=&#34;(max-width: 267px) 100vw, 267px&#34; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&#34;http://www.heroku.com/&#34; target=&#34;_blank&#34;&gt;Heroku&lt;/a&gt;&amp;nbsp;is a great platform. Long before I joined and when I say long, I mean in startup terms (&lt;em&gt;i.e. a few weeks before I joined :)&lt;/em&gt;) &amp;#8211; the decision was made that CrowdHired would be hosted on Heroku. Shortly after I came on board, Heroku released their new &lt;a href=&#34;http://blog.heroku.com/archives/2011/5/31/celadon_cedar/&#34; target=&#34;_blank&#34;&gt;Cedar stack&lt;/a&gt;&amp;nbsp;and we quickly migrated across to that. I find it kinda amusing that &lt;a href=&#34;https://www.crowdhired.com/&#34; target=&#34;_blank&#34;&gt;we&amp;rsquo;re currently in alpha&lt;/a&gt;, deploying to a platform that&amp;rsquo;s in beta. Latest and greatest FTW. While migrating to the new stack we also settled on &lt;a href=&#34;http://code.macournoyer.com/thin/&#34; target=&#34;_blank&#34;&gt;Thin&lt;/a&gt;&amp;nbsp;as our web server. The Cedar stack allows you to use &lt;a href=&#34;http://devcenter.heroku.com/articles/cedar&#34; target=&#34;_blank&#34;&gt;whatever web server you want in production&lt;/a&gt;&amp;nbsp;and will run on Webrick by default &amp;#8211; not ideal. Since we were going to use Thin in production it made sense that we&amp;rsquo;d also use it in development instead of Webrick.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When you&amp;rsquo;re using Rails, swapping a new web server in is pretty painless&lt;/strong&gt;. Just include the gem and then use the &lt;code&gt;rails s&lt;/code&gt; command to launch your new server e.g&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;rails s thin```

Pretty simple right? Well, it is, but I am very used to typing `rails s` to launch my server and no matter what gem you include in your project, `rails s` still starts Webrick (_this is not entirely accurate but bare with me_). Muscle memory being what it is, after typing `rails s` instead of `rails s thin` a couple of times (_and only realising this a few hours later_) I decided to see if I could make Thin the default for the `rails s` command.

## Digging Into The Rails Command Line

The key thing here was to figure out how `rails s` actually works &amp;amp;#8211; only way to do that is to &amp;lt;a href=&amp;#34;http://www.skorks.com/2010/05/why-i-love-reading-other-peoples-code-and-you-should-too/&amp;#34; target=&amp;#34;_blank&amp;#34;&amp;gt;read some code&amp;lt;/a&amp;gt;. We know that there is a `script/rails` executable that lives in all our Rails project so it makes sense that this would be the entry point into figuring out `rails s`, but in reality it&amp;#39;s not (_or at least it&amp;#39;s not that simple_). We don&amp;#39;t actually type `script/rails s`, we do `rails s`, so **there must be an executable called `rails` within the Rails gem itself** which is declared as such in rails&amp;#39; gemspec. This is indeed the case, it looks like this:

```ruby
#!/usr/bin/env ruby
 
if File.exists?(File.join(File.expand_path(&amp;#39;../..&amp;#39;, __FILE__), &amp;#39;.git&amp;#39;))
  railties_path = File.expand_path(&amp;#39;../../railties/lib&amp;#39;, __FILE__)
  $:.unshift(railties_path)
end
require &amp;#34;rails/cli&amp;#34;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;But even that is not the start of the story. Apparently, &lt;strong&gt;when you have an executable in a gem, rubygems will not use it as is, but will generate another executable which wraps your one.&lt;/strong&gt; For the rails command it looks like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;ch&#34;&gt;#!/usr/bin/env ruby&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;#&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;# This file was generated by RubyGems.&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;#&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;# The application &amp;#39;rails&amp;#39; is installed as part of a gem, and&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;# this file is here to facilitate running it.&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;#&lt;/span&gt;
 
&lt;span class=&#34;nb&#34;&gt;require&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;rubygems&amp;#39;&lt;/span&gt;
 
&lt;span class=&#34;n&#34;&gt;version&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;&amp;amp;gt;= 0&amp;#34;&lt;/span&gt;
 
&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;ARGV&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;first&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=~&lt;/span&gt; &lt;span class=&#34;sr&#34;&gt;/^_(.*)_$/&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;and&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;Gem&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Version&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;correct?&lt;/span&gt; &lt;span class=&#34;vg&#34;&gt;$1&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;then&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;version&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;vg&#34;&gt;$1&lt;/span&gt;
  &lt;span class=&#34;no&#34;&gt;ARGV&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;shift&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
 
&lt;span class=&#34;n&#34;&gt;gem&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;rails&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;version&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;load&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;Gem&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;bin_path&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;rails&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;rails&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;version&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This is the true entry point you hit when you type &lt;code&gt;rails s&lt;/code&gt;. Of course, all this does is load/call the original executable from the Rails gem.&lt;/p&gt;

&lt;p&gt;The Rails source is broken up into several projects such as &lt;em&gt;&lt;strong&gt;activerecord&lt;/strong&gt;&lt;/em&gt;, &lt;em&gt;&lt;strong&gt;activesupport&lt;/strong&gt;&lt;/em&gt; etc. Probably the most important one of these is &lt;em&gt;&lt;strong&gt;railties&lt;/strong&gt;&lt;/em&gt;. This is where the &lt;code&gt;rails&lt;/code&gt; executable takes us. Of course, before it does that it needs to put the &lt;code&gt;lib/&lt;/code&gt; folder of the &lt;em&gt;&lt;strong&gt;railties&lt;/strong&gt;&lt;/em&gt; project on the load path, but eventually we end up in &lt;code&gt;railties/lib/rails/cli.rb&lt;/code&gt;. Here we almost immediately execute the following:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;no&#34;&gt;Rails&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;ScriptRailsLoader&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;exec_script_rails!&lt;/span&gt;&lt;span class=&#34;sb&#34;&gt;```
&lt;/span&gt;&lt;span class=&#34;sb&#34;&gt;
&lt;/span&gt;&lt;span class=&#34;sb&#34;&gt;All this does is essentially figure out if we&amp;#39;re inside a Rails app and if we are it executes `&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;script&lt;/span&gt;&lt;span class=&#34;sr&#34;&gt;/rails` passing through the command line arguments that you supply. So, we&amp;#39;re now back in our Rails app; `script/&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;rails&lt;/span&gt;&lt;span class=&#34;sb&#34;&gt;` is the real entry point after all (_although we&amp;#39;re about to be taken straight back to **railties**_). The file looks like this:
&lt;/span&gt;&lt;span class=&#34;sb&#34;&gt;
&lt;/span&gt;&lt;span class=&#34;sb&#34;&gt;```&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ruby&lt;/span&gt;
&lt;span class=&#34;ch&#34;&gt;#!/usr/bin/env ruby&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;# This command will automatically be run when you run &amp;#34;rails&amp;#34; with Rails 3 gems installed from the root of your application.&lt;/span&gt;
 
&lt;span class=&#34;no&#34;&gt;APP_PATH&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;File&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;expand_path&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;../../config/application&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;  &lt;span class=&#34;bp&#34;&gt;__FILE__&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;require&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;File&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;expand_path&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;../../config/boot&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;  &lt;span class=&#34;bp&#34;&gt;__FILE__&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;require&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;rails/commands&amp;#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;We &lt;code&gt;require boot.rb&lt;/code&gt; so that we can hook into Bundler and make sure the relevant gems are on the load path (&lt;em&gt;such as rails for example&lt;/em&gt;), we then jump into &lt;code&gt;railties/rails/lib/commands.rb&lt;/code&gt;. Here everything is pretty straight forward, we have a big case statement which has a clause for &amp;ldquo;&lt;em&gt;server&lt;/em&gt;&amp;rdquo;. We instantiate a new &lt;code&gt;Rails::Server&lt;/code&gt; and start it, which tells us very little by itself, but if we jump into &lt;code&gt;railties/rails/lib/commands/server.rb&lt;/code&gt; we can see that &lt;code&gt;Rails::Server&lt;/code&gt; simply extends &lt;code&gt;Rack::Server&lt;/code&gt; (&lt;strong&gt;&lt;em&gt;and delegates to &lt;code&gt;Rack::Server&amp;amp;#39;s&lt;/code&gt; start method from its start method&lt;/em&gt;&lt;/strong&gt;) all it adds is those familiar lines we&amp;rsquo;re all used to seeing e.g.:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;=&amp;amp;gt; Booting Thin
=&amp;amp;gt; Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=&amp;amp;gt; Call with -d to detach
=&amp;amp;gt; Ctrl-C to shutdown server&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;So, if we want to change which server is picked up by default when we type &lt;code&gt;rails s&lt;/code&gt; we need to go look in Rack.&lt;/p&gt;

&lt;h2 id=&#34;a-quick-glance-at-rack&#34;&gt;A Quick Glance At Rack&lt;/h2&gt;

&lt;p&gt;Luckily we can easily grab it from &lt;a href=&#34;https://github.com/rack/rack&#34; target=&#34;_blank&#34;&gt;Github&lt;/a&gt;&amp;nbsp;and crack it open (&lt;strong&gt;&lt;em&gt;you have to admire the Ruby open source ecosystem, it is truly awesome, largely due to Github, so big props to those guys&lt;/em&gt;&lt;/strong&gt;). We of course need to check out &lt;code&gt;lib/rack/server.rb&lt;/code&gt; where we find the following method:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;server&lt;/span&gt;
  &lt;span class=&#34;vi&#34;&gt;@_server&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;||=&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;ss&#34;&gt;:server&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;||&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;So, if we don&amp;rsquo;t pass in the name of the server we want, &lt;code&gt;Rack::Handler.default&lt;/code&gt; will try to determine it for us.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{})&lt;/span&gt;
  &lt;span class=&#34;c1&#34;&gt;# Guess.&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;ENV&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;include?&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;PHP_FCGI_CHILDREN&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;# We already speak FastCGI&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delete&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:File&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delete&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:Port&lt;/span&gt;
 
    &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;FastCGI&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;elsif&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;ENV&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;include?&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;REQUEST_METHOD&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;CGI&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
      &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Mongrel&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;LoadError&lt;/span&gt;
      &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;WEBrick&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;As you can see, it turns out that the real default server is actually Mongrel. So if you included Mongrel in your Rails project, typing &lt;code&gt;rails s&lt;/code&gt; would automatically pick that up without you having to do anything. Only if Mongrel fails, do we fall back to Webrick which is part of Ruby and therefore is always present. So what do we do if we want Thin to be one of the defaults? Well, first thing first, we need to check if Rack includes a handler for Thin. If we look in &lt;code&gt;lib/rack/handlers/&lt;/code&gt; we can see that Rack itself includes the following:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;9
&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;cgi.rb
evented_mongrel.rb
fastcgi.rb
lsws.rb
mongrel.rb
scgi.rb
swiftiplied_mongrel.rb
thin.rb
webrick.rb&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Luckily Thin is there, so what we really want is that default method to look something like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{})&lt;/span&gt;
  &lt;span class=&#34;c1&#34;&gt;# Guess.&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;ENV&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;include?&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;PHP_FCGI_CHILDREN&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;# We already speak FastCGI&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delete&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:File&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delete&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:Port&lt;/span&gt;
 
    &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;FastCGI&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;elsif&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;ENV&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;include?&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;REQUEST_METHOD&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;CGI&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
      &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Thin&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;LoadError&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
        &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Mongrel&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;LoadError&lt;/span&gt;
        &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;WEBrick&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This way Thin will be the first default, followed by Mongrel and only then falling through to Webrick. Luckily, since we&amp;rsquo;re using Ruby, we can reopen the class and replace the method with our version. I think &lt;strong&gt;this is a perfect example where the ability to reopen classes comes in extremely handy, without any need to worry about &amp;ldquo;&lt;em&gt;scary consequences&lt;/em&gt;&amp;ldquo;&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&#34;getting-it-working&#34;&gt;Getting It Working&lt;/h2&gt;

&lt;p&gt;All we really need to figure out is where to put the code that reopens the class so that it gets picked up before Rails tries to launch the server. The only logical place is the &lt;code&gt;script/rails&lt;/code&gt; executable itself, which will now look like this:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-ruby&#34; data-lang=&#34;ruby&#34;&gt;&lt;span class=&#34;ch&#34;&gt;#!/usr/bin/env ruby&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;# This command will automatically be run when you run &amp;#34;rails&amp;#34; with Rails 3 gems installed from the root of your application.&lt;/span&gt;
 
&lt;span class=&#34;no&#34;&gt;APP_PATH&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;File&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;expand_path&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;../../config/application&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;  &lt;span class=&#34;bp&#34;&gt;__FILE__&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;require&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;File&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;expand_path&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;../../config/boot&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;  &lt;span class=&#34;bp&#34;&gt;__FILE__&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;require&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;rack/handler&amp;#39;&lt;/span&gt;
&lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;class_eval&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;do&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nf&#34;&gt;default&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;options&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{})&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;# Guess.&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;ENV&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;include?&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;PHP_FCGI_CHILDREN&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
      &lt;span class=&#34;c1&#34;&gt;# We already speak FastCGI&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delete&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:File&lt;/span&gt;
      &lt;span class=&#34;n&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;delete&lt;/span&gt; &lt;span class=&#34;ss&#34;&gt;:Port&lt;/span&gt;
 
      &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;FastCGI&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;elsif&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;ENV&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;include?&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;REQUEST_METHOD&amp;#34;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
      &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;CGI&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;else&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
        &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Mongrel&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;LoadError&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;begin&lt;/span&gt;
          &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Thin&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;rescue&lt;/span&gt; &lt;span class=&#34;no&#34;&gt;LoadError&lt;/span&gt;
          &lt;span class=&#34;no&#34;&gt;Rack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;Handler&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;::&lt;/span&gt;&lt;span class=&#34;no&#34;&gt;WEBrick&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;end&lt;/span&gt;
&lt;span class=&#34;nb&#34;&gt;require&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;rails/commands&amp;#39;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This works without any problems, we type &lt;code&gt;rails s&lt;/code&gt; and as long as Thin is in our Gemfile it starts up by default. As an aside, notice that I used &lt;code&gt;class_eval&lt;/code&gt; to reopen &lt;code&gt;Rack::Handler&lt;/code&gt;. Metaprogramming tricks like this should be part of every Ruby developer&amp;rsquo;s toolbox, I&amp;rsquo;ll talk more about this some other time&amp;nbsp;(&lt;em&gt;seeing as I am well into TL;DR land here&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Going through this exercise didn&amp;rsquo;t take long (&lt;em&gt;under 30 minutes&lt;/em&gt;) and taught me a bit more about Rails and Rack. Shortly after doing this &amp;#8211; in a curious case of the universe working in interesting ways &amp;#8211; I came across a &lt;a href=&#34;http://stackoverflow.com/q/4853393/639386&#34; target=&#34;_blank&#34;&gt;Stackoverflow question asking about this exact scenario&lt;/a&gt;&amp;nbsp;and got an inordinate amount of satisfaction from being able to easily answer it :). In-fact, even the fact that shortly after &lt;a href=&#34;https://www.crowdhired.com/about_us?show_tab=jason_nah&#34; target=&#34;_blank&#34;&gt;Jason&lt;/a&gt;&amp;nbsp;found &lt;a href=&#34;http://pow.cx/&#34; target=&#34;_blank&#34;&gt;Pow&lt;/a&gt;&amp;nbsp;and we switched over to that, doesn&amp;rsquo;t really diminish the satisfaction of quickly solving a seemingly difficult problem in a neat way. The lesson here is this, &lt;strong&gt;no matter what problems you come across don&amp;rsquo;t automatically look for a library to handle it&lt;/strong&gt;. Do spend a few minutes investigating &amp;#8211; it might be enough to solve it (&lt;em&gt;especially if you&amp;rsquo;re using Ruby&lt;/em&gt;) and you&amp;rsquo;ll certainly learn something.&lt;/p&gt;

&lt;p&gt;&lt;span style=&#34;font-size: 10px; font-family: trebuchet ms;&#34;&gt;Image by &lt;a href=&#34;http://www.flickr.com/photos/gerardstolk/2327590838/&#34; target=&#34;_blank&#34;&gt;Gerard Stolk presque 64&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
</description>
      
    </item>
    
  </channel>
</rss>