<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Gray Soft / Tags / APIs</title>
  <id>tag:graysoftinc.com,2014-03-20:/tags/APIs</id>
  <updated>2014-04-29T03:29:32Z</updated>
  <link rel="self" href="http://graysoftinc.com/tags/APIs/feed.xml"/>
  <link rel="alternate" href="http://graysoftinc.com/tags/APIs"/>
  <author>
    <name>James Edward Gray II</name>
  </author>
  <entry>
    <title>Delaying Decisions</title>
    <link rel="alternate" href="http://graysoftinc.com/rubies-in-the-rough/delaying-decisions"/>
    <id>tag:graysoftinc.com,2012-05-11:/posts/114</id>
    <updated>2014-04-29T03:29:32Z</updated>
    <summary>In this article I talk about the value of delaying programming decisions and look at some ways you can implement code to do that.</summary>
    <content type="html">&lt;p&gt;I love playing with Ruby's &lt;code&gt;Hash&lt;/code&gt;.  I think it has a neat API and experimenting with it can actually help you understand how to write good Ruby.  Let's dig into this idea to see what I mean.&lt;/p&gt;

&lt;h4&gt;The nil Problem&lt;/h4&gt;

&lt;p&gt;In &lt;a href="https://www.destroyallsoftware.com/screencasts/catalog/exceptions-and-control-flow"&gt;Destroy All Software #9&lt;/a&gt; Gary chooses to show an example in Python because, unlike Ruby's &lt;code&gt;Hash&lt;/code&gt;, it will raise an error for a non-existent key.  Ruby just returns &lt;code&gt;nil&lt;/code&gt;, he explains.&lt;/p&gt;

&lt;p&gt;What Gary said isn't really true, but I'm guessing he just didn't know that at the time.  He was in the process of switching to Ruby from Python and I'm guessing he just didn't have a deep enough understanding of Ruby's &lt;code&gt;Hash&lt;/code&gt; yet.  I bet he does know how it works now.&lt;/p&gt;

&lt;p&gt;But assume he was right.  What's he saying and why does it matter?  Consider some code like this:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SearchesController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show&lt;/span&gt;
    &lt;span class="n"&gt;terms&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:terms&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    &lt;span class="no"&gt;SomeModel&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is what Gary doesn't like, and rightfully so.  Because I indexed into &lt;code&gt;params&lt;/code&gt; here with the &lt;code&gt;[]()&lt;/code&gt; method, I will indeed get a &lt;code&gt;nil&lt;/code&gt; if the &lt;code&gt;:terms&lt;/code&gt; key wasn't in &lt;code&gt;params&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The problem with &lt;code&gt;nil&lt;/code&gt; is that it's a ticking time bomb just waiting to go off.  It has a very limited interface by design, so calling methods on it may raise errors.  It's probably not the interface your code is expecting.  For example, in the code above I likely intended to get a &lt;code&gt;String&lt;/code&gt; in &lt;code&gt;terms&lt;/code&gt;, but if I got &lt;code&gt;nil&lt;/code&gt; instead and I later try to call &lt;code&gt;String&lt;/code&gt; methods on it, it's going to blow up.&lt;/p&gt;

&lt;p&gt;This gets worse because &lt;code&gt;nil&lt;/code&gt; values tend to bounce around in the code a bit.  This means the error may finally manifest far from the line where I actually assigned &lt;code&gt;terms&lt;/code&gt;, which is the real problem that needs fixing.  For example, let's say the model code I handed off to looks something like this:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SomeModel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/\S+/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# ...&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is where the error would show up.  The call to &lt;code&gt;scan()&lt;/code&gt; would toss a &lt;code&gt;NoMethodError&lt;/code&gt; since &lt;code&gt;nil&lt;/code&gt; doesn't have &lt;code&gt;scan()&lt;/code&gt;.  The stack trace would lead us here, but, as I said before, this isn't what needs fixing.&lt;/p&gt;

&lt;p&gt;Now, we &lt;em&gt;could&lt;/em&gt; fix it here.  One way to do that would be to insert a conversion before the &lt;code&gt;scan()&lt;/code&gt; call:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/\S+/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That's probably not a good idea though.  We've now said that calling &lt;code&gt;SomeModel.search(nil)&lt;/code&gt; is OK and that doesn't really make much sense.&lt;/p&gt;

&lt;p&gt;A slightly better fix would be to add a guard before the call to &lt;code&gt;scan()&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="nb"&gt;fail&lt;/span&gt; &lt;span class="no"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"You must pass search terms"&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nil?&lt;/span&gt;
&lt;span class="n"&gt;words&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;terms&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/\S+/&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This would get us a better error message that actually tells us what went wrong.  But the stack trace still wouldn't be ideal.  It's going to bring us here first even though the real issue is in the controller.&lt;/p&gt;

&lt;h5&gt;My Favorite Method to the Rescue&lt;/h5&gt;

&lt;p&gt;This whole problem started because I allowed the &lt;code&gt;Hash&lt;/code&gt; to give me a &lt;code&gt;nil&lt;/code&gt;.  I didn't want a &lt;code&gt;nil&lt;/code&gt;, so I shouldn't have said it was OK.&lt;/p&gt;

&lt;p&gt;That brings us to &lt;code&gt;fetch()&lt;/code&gt;.  Watch this:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:missing&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:missing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="ss"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="ss"&gt;found&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="ss"&gt;:missing&lt;/span&gt;
&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pry&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="ss"&gt;:in&lt;/span&gt; &lt;span class="sb"&gt;`fetch'&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can think of &lt;code&gt;[]()&lt;/code&gt; as saying give me some key, if it exists.  You get a &lt;code&gt;nil&lt;/code&gt; if it doesn't.  Now &lt;code&gt;fetch()&lt;/code&gt;, on the other hand, is more like saying I need this key, no substitutions allowed.  This forces &lt;code&gt;fetch()&lt;/code&gt; to raise a &lt;code&gt;KeyError&lt;/code&gt; when the key is missing, since &lt;code&gt;nil&lt;/code&gt; isn't allowed.&lt;/p&gt;

&lt;p&gt;The best fix to the problem I started with would be to replace the initial assignment in the controller with this:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;terms&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:terms&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You may also want to add some code to handle the &lt;code&gt;KeyError&lt;/code&gt;, but the important thing is that the problem now triggers from the proper place.  This is the most helpful error we could work with in this scenario.&lt;/p&gt;

&lt;h4&gt;I Don't Know What You Need&lt;/h4&gt;

&lt;p&gt;Let's take a step back for a moment to the overall &lt;code&gt;Hash&lt;/code&gt;.  What exactly is a &lt;code&gt;Hash&lt;/code&gt; in your code?  The only right answer is that I have no idea.&lt;/p&gt;

&lt;p&gt;You might be creating a typical key-to-value mapping:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"James"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Gray"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:first&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"James"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:last&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;"Gray"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:last&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"Gray"&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Alternately, you could be tracking some counts:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;counts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:one&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;times&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:three&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;counts&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;values_at&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:zero&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:one&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:three&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Or you could be keeping some named buckets for data:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;buckets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;buckets&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:one&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;buckets&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:three&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;buckets&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:one&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:three&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Perhaps it's even a deeply nested structure that you wish to track:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;default_proc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:deeply&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:nested&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:structure&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tree&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:deeply&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:nested&lt;/span&gt;&lt;span class="o"&gt;][&lt;/span&gt;&lt;span class="ss"&gt;:branch&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;tree&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:deeply&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:nested&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:structure&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:branch&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;}}}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These are all very different, but valid, uses of a &lt;code&gt;Hash&lt;/code&gt; in Ruby to model various data structures and there are other options too.  Ruby can't know what you intend to do.  But it was wisely designed so that it doesn't have to know.&lt;/p&gt;

&lt;p&gt;Note the progression above that makes this possible.  You can create a normal &lt;code&gt;Hash&lt;/code&gt;.  You can pass a default object, like an initialization value for counters.  Or you can go all the way to defining the default behavior with custom code inside of a block as I did in the last two examples.&lt;/p&gt;

&lt;p&gt;Ruby delays the decision of default behavior so that it can leave that decision to you.  You know what you need better than Ruby does.&lt;/p&gt;

&lt;p&gt;And it doesn't stop there.&lt;/p&gt;

&lt;h5&gt;Back to My Favorite&lt;/h5&gt;

&lt;p&gt;Being able to set some default behavior at &lt;code&gt;Hash&lt;/code&gt; construction time is great, but what if I don't know everything I need to know even then?  What if I need to delay the decision even more, until key lookup time?  What if what I actually need is different at different times?  The answer is that &lt;code&gt;fetch()&lt;/code&gt; can handle those cases too.&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:missing&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:missing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sort&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"giimnss"&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can see that &lt;code&gt;fetch()&lt;/code&gt; also supports default return values for a missing keys and it will even allow you to provide custom code to handle the situation however you need to.&lt;/p&gt;

&lt;p&gt;If you only get one thing out of this article, make it &lt;code&gt;Hash#fetch()&lt;/code&gt;.  It's a powerhouse tool that enables you to do all kinds of fancy tricks.  (It's worth noting that &lt;code&gt;Hash#delete()&lt;/code&gt; is similarly flexible.  &lt;a href="http://ruby-doc.org/core-1.9.3/Hash.html#method-i-delete"&gt;Look it up&lt;/a&gt;.)  But that's not really why I'm showing you these methods.&lt;/p&gt;

&lt;h4&gt;The Pattern is the Thing&lt;/h4&gt;

&lt;p&gt;The real reason to play with these methods is that they represent a great tactic for Ruby programming in general.  It's important to remember that you won't know exactly what the users of your code will need.  Delay making that decision whenever you can.&lt;/p&gt;

&lt;p&gt;In fact, delay it as long as you can, ideally just pushing the decision off on the user when they finally need to make it.  They will know what they need better than you do.  Let them raise an error, do a conversion, or whatever else makes sense for them.&lt;/p&gt;

&lt;p&gt;In a lot of ways, this discussion is about Ruby's blocks.  That's the great tool Ruby gives us to allow us to delay these decisions.  Consider even a simple method like this:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;do_some_io_operations_for_me&lt;/span&gt;
  &lt;span class="c1"&gt;# ... IO code here...&lt;/span&gt;
&lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="no"&gt;IOError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;Errno&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;EACCES&lt;/span&gt; &lt;span class="c1"&gt;#, ...&lt;/span&gt;
  &lt;span class="nb"&gt;block_given?&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;raise&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This is a pretty perfect setup, in my opinion.  This code tries to push through some &lt;code&gt;IO&lt;/code&gt; operations.  Those could fail for any number of reasons, so the code intelligently traps the errors that apply.  If a users gives a block, they can control what happens on error without even needing to know which errors the &lt;code&gt;IO&lt;/code&gt; code could trigger.  However, the decision of exactly what to do is left to the caller.&lt;/p&gt;

&lt;p&gt;Of course, the biggest limitation on this strategy is that we only get one block in Ruby.  Once you delay one decision with it, you won't have it for other purposes.  Try not to let that stop you though!  Ruby doesn't.&lt;/p&gt;

&lt;p&gt;Consider the &lt;code&gt;find()&lt;/code&gt; iterator.  With &lt;code&gt;find()&lt;/code&gt; the block is already tied up for the test, but Ruby wants to let you decide what happens when nothing is found.  Returning &lt;code&gt;nil&lt;/code&gt; is not enough for those cases, because &lt;code&gt;nil&lt;/code&gt; could legitimately be what the block found.  Because of that, &lt;code&gt;find()&lt;/code&gt; cheats to expose kind of a second block:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;fail&lt;/span&gt; &lt;span class="no"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Not found"&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="ss"&gt;ArgumentError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Not&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt;
&lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pry&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="ss"&gt;:in&lt;/span&gt; &lt;span class="sb"&gt;`block in &amp;lt;main&amp;gt;'&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I think these kind of tricks come out a little cleaner with the new "stabby &lt;code&gt;lambda()&lt;/code&gt;" (&lt;code&gt;-&amp;gt;&lt;/code&gt;) syntax that I used above.&lt;/p&gt;

&lt;h4&gt;Further Investigation&lt;/h4&gt;

&lt;p&gt;Fully getting the hang of when and where to use blocks in Ruby seems to take almost as long to get a feel for as the rest of the language does.  Here are some sources that might shave a little time off of the journey though:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I wrote &lt;a href="/ruby-tutorials/code-as-a-data-type"&gt;an article&lt;/a&gt; on blocks a long time ago.  I think it has aged fairly well and still provides some insight into why blocks exist and what they really are.&lt;/li&gt;
&lt;li&gt;Rake is a great example of the delayed decisions I am advocating in this article in many ways.  When you think about it, Rakes is really just an executable dependency graph of delayed decisions.  For a more concrete example though, check out Rake's &lt;a href="http://martinfowler.com/articles/rake.html#FileTasks"&gt;FileTask&lt;/a&gt;.  It's a delayed decision about how to build a file from one or more dependencies any time the content is no longer fresh.&lt;/li&gt;
&lt;li&gt;The 2.10.0 release of RSpec brings with it &lt;a href="https://github.com/rspec/rspec-expectations#yielding"&gt;some matchers for yielded control to a block&lt;/a&gt;.  This can make testing all of those block methods I'm asking you to write a little easier.&lt;/li&gt;
&lt;/ul&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
  <entry>
    <title>Dual Interface Modules</title>
    <link rel="alternate" href="http://graysoftinc.com/ruby-voodoo/dual-interface-modules"/>
    <id>tag:graysoftinc.com,2008-10-09:/posts/62</id>
    <updated>2014-04-11T19:28:44Z</updated>
    <summary>Two different ways to use modules with one simple trick.</summary>
    <content type="html">&lt;p&gt;I'm guessing we've all seen Ruby's &lt;code&gt;Math&lt;/code&gt; &lt;code&gt;Module&lt;/code&gt;.  I'm sure you know that you can call methods in it as "module (or class) methods:"&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="no"&gt;Math&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; 2.0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That's just one way to use the &lt;code&gt;Math&lt;/code&gt; &lt;code&gt;Module&lt;/code&gt; though.  Another is to treat it as a mixin and call the same methods as instance methods:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;MyMathyThing&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;Math&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;my_sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;MyMathyThing&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; 2.0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Ruby ships with a few &lt;code&gt;Module&lt;/code&gt;s that work like this, including the mighty &lt;code&gt;Kernel&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How is this dual interface accomplished?  With the seldom seen &lt;code&gt;module_function()&lt;/code&gt; method.  You use this much like you would &lt;code&gt;private()&lt;/code&gt;, to affect all following method definitions:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Greeter&lt;/span&gt;
  &lt;span class="kp"&gt;module_function&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;
    &lt;span class="s2"&gt;"Hello!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;MyGreeter&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;Greeter&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;my_hello&lt;/span&gt;
    &lt;span class="n"&gt;hello&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Greeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;       &lt;span class="c1"&gt;# =&amp;gt; "Hello!"&lt;/span&gt;
&lt;span class="no"&gt;MyGreeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_hello&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; "Hello!"&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As you can see, it magically gives us the dual interface for the methods beneath it.  You can also affect specific methods by name, just as you could with &lt;code&gt;private()&lt;/code&gt;.  This is equivalent to my definition above:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Greeter&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;
    &lt;span class="s2"&gt;"Hello!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="kp"&gt;module_function&lt;/span&gt; &lt;span class="ss"&gt;:hello&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;What this helper actually does is to make a copy of the method and move it up to the module interface level.  Once the copy is made, they can be affected separately:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Copies&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;copy&lt;/span&gt;
    &lt;span class="s2"&gt;"Copied!"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="kp"&gt;module_function&lt;/span&gt; &lt;span class="ss"&gt;:copy&lt;/span&gt;

  &lt;span class="n"&gt;alias_method&lt;/span&gt; &lt;span class="ss"&gt;:copier&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:copy&lt;/span&gt;
  &lt;span class="kp"&gt;public&lt;/span&gt;       &lt;span class="ss"&gt;:copier&lt;/span&gt;
  &lt;span class="k"&gt;undef&lt;/span&gt;        &lt;span class="ss"&gt;:copy&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Copies&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; "Copied!"&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extend&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Copies&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copier&lt;/span&gt;     &lt;span class="c1"&gt;# =&amp;gt; "Copied!"&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;copy&lt;/span&gt;
&lt;span class="c1"&gt;# ~&amp;gt; -:16: undefined method `copy' for #&amp;lt;Object:0x26e6c&amp;gt; (NoMethodError)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This process also marks the instance method version &lt;code&gt;private()&lt;/code&gt;, which is why I needed the call to &lt;code&gt;public()&lt;/code&gt; in the last example.  This means that methods you mixin to another object do not add to its external interface:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Selfish&lt;/span&gt;
  &lt;span class="kp"&gt;module_function&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;mine&lt;/span&gt;
    &lt;span class="s2"&gt;"mine"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Sharing&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;Selfish&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;yours_and_mine&lt;/span&gt;
    &lt;span class="s2"&gt;"yours and &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="n"&gt;mine&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Sharing&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;yours_and_mine&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; "yours and mine"&lt;/span&gt;
&lt;span class="no"&gt;Sharing&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mine&lt;/span&gt;
&lt;span class="c1"&gt;# ~&amp;gt; -:18: private method `mine' called for Sharing:Module (NoMethodError)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;As we've seen there are some advantages to this interface, but there are some drawbacks too.  For example, I first tried to write the &lt;code&gt;MyGreeter&lt;/code&gt; example as:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;MyGreeter&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Greeter&lt;/span&gt;

  &lt;span class="kp"&gt;module_function&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_hello&lt;/span&gt;
    &lt;span class="n"&gt;hello&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;MyGreeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_hello&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; &lt;/span&gt;
&lt;span class="c1"&gt;# ~&amp;gt; -:15:in `my_hello': undefined local variable or method&lt;/span&gt;
&lt;span class="c1"&gt;# ~&amp;gt;      `hello' for MyGreeter:Module (NameError)&lt;/span&gt;
&lt;span class="c1"&gt;# ~&amp;gt;    from -:20&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That didn't work because the &lt;code&gt;Greeter&lt;/code&gt; functionality was not copied up with my method.  You can fix that by using a different trick to duplicate the functionality:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;MyGreeter&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Greeter&lt;/span&gt;

  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;  &lt;span class="c1"&gt;# mixin functionality to our own Module interface&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;my_hello&lt;/span&gt;
    &lt;span class="n"&gt;hello&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;MyGreeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_hello&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; "Hello!"&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This provides a similar dual interface, but there are important differences.  First, we've changed the &lt;code&gt;ancestors()&lt;/code&gt; of &lt;code&gt;MyGreeter&lt;/code&gt;, not copied methods:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="no"&gt;MyGreeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ancestors&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; [MyGreeter, Greeter]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There's just one method and changing it affects everywhere it is used.&lt;/p&gt;

&lt;p&gt;We also didn't magically make the mixin interface &lt;code&gt;private()&lt;/code&gt; and it will bleed through:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;MyNestedGreeter&lt;/span&gt;
  &lt;span class="kp"&gt;extend&lt;/span&gt; &lt;span class="no"&gt;MyGreeter&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;my_nested_hello&lt;/span&gt;
    &lt;span class="n"&gt;my_hello&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;MyNestedGreeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_nested_hello&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; "Hello!"&lt;/span&gt;
&lt;span class="no"&gt;MyNestedGreeter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;my_hello&lt;/span&gt;         &lt;span class="c1"&gt;# =&amp;gt; "Hello!"&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It's all tradeoffs of course, but knowing our options allows us to make informed choices about what is best for our needs.&lt;/p&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
  <entry>
    <title>Readable Booleans</title>
    <link rel="alternate" href="http://graysoftinc.com/ruby-voodoo/readable-booleans"/>
    <id>tag:graysoftinc.com,2008-10-08:/posts/61</id>
    <updated>2014-04-11T19:19:30Z</updated>
    <summary>Here's a quick tip for how to make your methods calls even more readable than the method writer intended.</summary>
    <content type="html">&lt;p&gt;There's a great little trick you can do to improve the readability of your code.  A common problem is dealing with methods that have a boolean flag arguments.  Here's an example I ran into just today in a Rails application:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;rating_stars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;,&lt;/span&gt; &lt;span class="n"&gt;clickable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="c1"&gt;# ...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The problem with this is that you typically see calls like this scattered around the application:&lt;/p&gt;

&lt;div class="highlight highlight-erb"&gt;&lt;pre&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;rating_stars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;,&lt;/span&gt; &lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="x"&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Would you know what &lt;code&gt;true&lt;/code&gt; did there if I hadn't shown you the name of the variable first?  I didn't.  I had to go hunting for that method definition.&lt;/p&gt;

&lt;p&gt;Ironically the opposite problem, a magical dangling &lt;code&gt;false&lt;/code&gt;, is much more rare in my experience.  That's typically the default for these kind of arguments and it just makes more sense and reads better to leave it out.&lt;/p&gt;

&lt;p&gt;Anyway, the point is that we can typically improve the ease of understanding the common case.  Remember that in Ruby &lt;code&gt;false&lt;/code&gt; and &lt;code&gt;nil&lt;/code&gt; are false while everything else is true.  That means that truth is very loosely defined and we can pass a lot of things for our boolean flag value.  For example, after looking up the method and understanding what was needed, I chose to call it like this:&lt;/p&gt;

&lt;div class="highlight highlight-erb"&gt;&lt;pre&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;rating_stars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;.&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:clickable&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="x"&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;My hope is that might save a future maintainer a trip to the method definition to understand the call.&lt;/p&gt;

&lt;p&gt;I've used that trick in quite a few situations now.  To give another example, &lt;code&gt;FasterCSV&lt;/code&gt;'s documentation encourages user code to pass &lt;code&gt;:headers =&amp;gt; :first_row&lt;/code&gt; instead of just &lt;code&gt;:headers =&amp;gt; true&lt;/code&gt;.  That has the advantage of being more self-documenting as well as leaving me room for possible expansion to the API.&lt;/p&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
  <entry>
    <title>Conversion Methods</title>
    <link rel="alternate" href="http://graysoftinc.com/ruby-voodoo/conversion-methods"/>
    <id>tag:graysoftinc.com,2008-10-06:/posts/59</id>
    <updated>2014-04-11T19:05:08Z</updated>
    <summary>There are quite a few things to know about the simple type conversion methods in Ruby.</summary>
    <content type="html">&lt;p&gt;I want to take a step back from all the syntax I've been covering lately and just talk about some simple methods in Ruby's core.  Ruby ships with so many great helpers, it's often hard to keep track of what everything can do.  Specifically, let's talk about the type conversion methods.&lt;/p&gt;

&lt;p&gt;I assume we all make calls to &lt;code&gt;to_s()&lt;/code&gt; and &lt;code&gt;to_i()&lt;/code&gt; regularly:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_s&lt;/span&gt;    &lt;span class="c1"&gt;# =&amp;gt; "255"&lt;/span&gt;
&lt;span class="s2"&gt;"255"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_i&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There shouldn't be any surprises there.  Even these two simple methods can do more though.  They make it possible to convert to and from various numeric bases.  For example, here are the same conversions into and out of base 16 (hexadecimal):&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_s&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;# =&amp;gt; "ff"&lt;/span&gt;
&lt;span class="s2"&gt;"ff"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_i&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Ruby has other ways to do these same conversions.  Here are two unusual methods (beginning with capital letters) that are similar:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;     &lt;span class="c1"&gt;# =&amp;gt; "255"&lt;/span&gt;
&lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"255"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I'll be honest and tell you that I don't really find &lt;code&gt;String()&lt;/code&gt; useful as it just calls &lt;code&gt;to_s()&lt;/code&gt; for you, but &lt;code&gt;Integer()&lt;/code&gt; is a different story.  First of all, &lt;code&gt;to_i()&lt;/code&gt; is very lenient about what it converts while &lt;code&gt;Integer()&lt;/code&gt; is more strict:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="s2"&gt;"C#"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_i&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 0&lt;/span&gt;
&lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"C#"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ~&amp;gt; -:5:in `Integer': invalid value for Integer: "C#" (ArgumentError)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That can be handy when you want to be sure you you have a number.&lt;/p&gt;

&lt;p&gt;Now while &lt;code&gt;Integer()&lt;/code&gt; must be passed a number, it understands numbers in all of the native formats Ruby does:  decimal, octal, hexadecimal, and binary.  In comparison, &lt;code&gt;to_i()&lt;/code&gt; just understands decimal integers:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="mi"&gt;255&lt;/span&gt;                    &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;span class="mo"&gt;0377&lt;/span&gt;                   &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;span class="mh"&gt;0xFF&lt;/span&gt;                   &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;span class="m-Bin"&gt;0b11111111&lt;/span&gt;             &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;

&lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"255"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;         &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0377"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0xFF"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"0b11111111"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;

&lt;span class="s2"&gt;"255"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_i&lt;/span&gt;             &lt;span class="c1"&gt;# =&amp;gt; 255&lt;/span&gt;
&lt;span class="s2"&gt;"0377"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_i&lt;/span&gt;            &lt;span class="c1"&gt;# =&amp;gt; 377&lt;/span&gt;
&lt;span class="s2"&gt;"0xFF"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_i&lt;/span&gt;            &lt;span class="c1"&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;span class="s2"&gt;"0b11111111"&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_i&lt;/span&gt;      &lt;span class="c1"&gt;# =&amp;gt; 0&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;If you want to be sure you are passing in safe content to either method, you should probably verify the &lt;code&gt;String&lt;/code&gt; contents with a regular expression before you make the call.&lt;/p&gt;

&lt;p&gt;There is a &lt;code&gt;Float()&lt;/code&gt; method as well, but it doesn't give you much over &lt;code&gt;to_f()&lt;/code&gt;.  The only significant difference is how they handle &lt;code&gt;nil&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to_f&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 0.0&lt;/span&gt;
&lt;span class="nb"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# ~&amp;gt; -:10:in `Float': can't convert nil into Float (TypeError)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Numbers aren't the only thing we can convert in Ruby though.  Let's talk about &lt;code&gt;Array()&lt;/code&gt;.  Ruby use to have a direct &lt;code&gt;to_a()&lt;/code&gt; method on all objects, but it has been deprecated in Ruby 1.8:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;ruby&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ve&lt;/span&gt; &lt;span class="s1"&gt;'p 5.to_a'&lt;/span&gt;
&lt;span class="n"&gt;ruby&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2008&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;08&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt; &lt;span class="n"&gt;patchlevel&lt;/span&gt; &lt;span class="mi"&gt;287&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i686&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;darwin9&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="ss"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="ss"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;default&lt;/span&gt; &lt;span class="sb"&gt;`to_a' will be obsolete&lt;/span&gt;
&lt;span class="sb"&gt;[5]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;And it's finally gone in Ruby 1.9:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;ruby_dev&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ve&lt;/span&gt; &lt;span class="s1"&gt;'p 5.to_a'&lt;/span&gt;
&lt;span class="n"&gt;ruby&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2008&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;09&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;27&lt;/span&gt; &lt;span class="n"&gt;revision&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i386&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;darwin9&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="ss"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="ss"&gt;:in&lt;/span&gt; &lt;span class="sb"&gt;`&amp;lt;main&amp;gt;': undefined method `&lt;/span&gt;&lt;span class="nb"&gt;to_a&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="ss"&gt;:Fixnum&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;NoMethodError&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That leaves us with just &lt;code&gt;Array()&lt;/code&gt;, but that's not a bad thing at all since it is very handy.  What makes &lt;code&gt;Array()&lt;/code&gt; great is the three different behaviors it has on the objects you pass:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It has no effect on an &lt;code&gt;Array&lt;/code&gt; which is return unchanged&lt;/li&gt;
&lt;li&gt;It returns an empty &lt;code&gt;Array&lt;/code&gt; when passed &lt;code&gt;nil&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;All other objects are returned wrapped in an &lt;code&gt;Array&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Here are examples of each of those:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; [1, 2, 3]&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;        &lt;span class="c1"&gt;# =&amp;gt; []&lt;/span&gt;
&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;          &lt;span class="c1"&gt;# =&amp;gt; [5]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These simple rules mean that you can count on an &lt;code&gt;Array&lt;/code&gt; being returned from &lt;code&gt;Array()&lt;/code&gt;, no matter what you pass it.  Thus it is safe to call any &lt;code&gt;Array&lt;/code&gt; methods on the result, including iterators.&lt;/p&gt;

&lt;p&gt;To give a more practical example, let's say you have some parameter your Rails application is prepared to receive.  You may receive any number of items in this parameter:  zero, one, or many.  You can process them with a simple:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:choices&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;choice&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="c1"&gt;# process choice here...&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There's one last conversion method that I make regular use of and it is &lt;code&gt;Hash[]&lt;/code&gt;.  Note the brackets there as they aren't parentheses like the other methods we've been talking about.  This one is a class method on &lt;code&gt;Hash&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This method is very simple in function.  It takes an even number of arguments and creates a &lt;code&gt;Hash&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; {"a"=&amp;gt;1, "b"=&amp;gt;2}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;At first glance, that doesn't seem to gain us much over the normal &lt;code&gt;Hash&lt;/code&gt; literal syntax of &lt;code&gt;{ … }&lt;/code&gt;.  However, this is a method call we can use to build a &lt;code&gt;Hash&lt;/code&gt; and that means we can apply the normal tricks of method calls to it, like splatting an &lt;code&gt;Array&lt;/code&gt; of arguments:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;[*&lt;/span&gt;&lt;span class="sx"&gt;%w[a 1 b 2]&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; {"a"=&amp;gt;"1", "b"=&amp;gt;"2"}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That opens up all kinds of possibilities for converting from &lt;code&gt;Array&lt;/code&gt; objects to &lt;code&gt;Hash&lt;/code&gt; objects or for building iterations that result in a &lt;code&gt;Hash&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;[*&lt;/span&gt;&lt;span class="sx"&gt;%w[a 1 b 2 c 3]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nb"&gt;Integer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;rescue&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; {"a"=&amp;gt;1, "b"=&amp;gt;2, "c"=&amp;gt;3}&lt;/span&gt;
&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="no"&gt;Hash&lt;/span&gt;&lt;span class="o"&gt;[*&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;select&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nonzero?&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flatten&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; {"a"=&amp;gt;1, "c"=&amp;gt;3}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Notice how we get a &lt;code&gt;Hash&lt;/code&gt; out of the last line there when Ruby 1.8 would usually give us an &lt;code&gt;Array&lt;/code&gt; of &lt;code&gt;Array&lt;/code&gt; objects.  Ruby 1.9 modifies most &lt;code&gt;Hash&lt;/code&gt; iterators to return a &lt;code&gt;Hash&lt;/code&gt; though:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="n"&gt;ruby_dev&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;ve&lt;/span&gt; &lt;span class="s1"&gt;'p({"a"=&amp;gt;1, "b"=&amp;gt;2, "c"=&amp;gt;3}.select { |_, n| (n % 2).nonzero? })'&lt;/span&gt;
&lt;span class="n"&gt;ruby&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2008&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;09&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;27&lt;/span&gt; &lt;span class="n"&gt;revision&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i386&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;darwin9&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"a"&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"c"&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Hopefully that gives you some fresh ideas about how you might handle simple conversions in the future.&lt;/p&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
  <entry>
    <title>The One Method Config</title>
    <link rel="alternate" href="http://graysoftinc.com/ruby-voodoo/the-one-method-config"/>
    <id>tag:graysoftinc.com,2008-06-25:/posts/53</id>
    <updated>2014-04-11T14:49:50Z</updated>
    <summary>Here's a simple bit of Ruby magic for trivial configuration code.</summary>
    <content type="html">&lt;p&gt;I've used this technique a couple of times now for dirt-simple configurations.  The idea is to provide a trivial way to read and write configuration values with just a single method.  Let me show you what I mean:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Configurable&lt;/span&gt;
  &lt;span class="kp"&gt;module_function&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;new_config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nil?&lt;/span&gt;
      &lt;span class="vi"&gt;@config&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;merge!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_config&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Configurable&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt;                    &lt;span class="c1"&gt;# =&amp;gt; {}&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="ss"&gt;:a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:b&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="n"&gt;config&lt;/span&gt;                    &lt;span class="c1"&gt;# =&amp;gt; {:a=&amp;gt;1, :b=&amp;gt;2}&lt;/span&gt;
&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:a&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;                &lt;span class="c1"&gt;# =&amp;gt; 1&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="ss"&gt;:a&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:c&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;config&lt;/span&gt;                    &lt;span class="c1"&gt;# =&amp;gt; {:a=&amp;gt;-1, :b=&amp;gt;2, :c=&amp;gt;3}&lt;/span&gt;

&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;clear&lt;/span&gt;
&lt;span class="n"&gt;config&lt;/span&gt;                    &lt;span class="c1"&gt;# =&amp;gt; {}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;There's no deep magic here, obviously.  The method has two function:  read and write for the configuration.  Read is handled with what I like refer to as Ruby's "caching operator" (&lt;code&gt;||=&lt;/code&gt;).  The first time that line is triggered, it will cache an empty &lt;code&gt;Hash&lt;/code&gt; in the variable.  Thereafter, the same call is just a cache hit to get the same &lt;code&gt;Hash&lt;/code&gt; back.&lt;/p&gt;

&lt;p&gt;We could stop there, of course.  Access to the &lt;code&gt;Hash&lt;/code&gt; allows us to use any methods we need on it.  However, one more nicety really pays off, in my opinion.  Give an optional argument and, when it's available, use it as a shortcut for one of the write methods.  In this case, I chose &lt;code&gt;merge!()&lt;/code&gt; just because it's a well rounded tool for adding and/or editing multiple &lt;code&gt;Hash&lt;/code&gt; entries at once.&lt;/p&gt;

&lt;p&gt;The examples show how this plays out.  Note that I mix both styles of the method calls with some standard &lt;code&gt;Hash&lt;/code&gt; tools (&lt;code&gt;[]&lt;/code&gt; and &lt;code&gt;clear()&lt;/code&gt;).  I feel like that gives a whole lot of cool interface with barely any effort.&lt;/p&gt;

&lt;p&gt;One more example, just to get your brain spinning in other directions:&lt;/p&gt;

&lt;div class="highlight highlight-ruby"&gt;&lt;pre&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Sortable&lt;/span&gt;
  &lt;span class="kp"&gt;module_function&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sortable_fields&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty?&lt;/span&gt;
      &lt;span class="vi"&gt;@sortable_fields&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;
      &lt;span class="n"&gt;sortable_fields&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;fields&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flatten&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Sortable&lt;/span&gt;

&lt;span class="n"&gt;sortable_fields&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; []&lt;/span&gt;

&lt;span class="n"&gt;sortable_fields&lt;/span&gt; &lt;span class="ss"&gt;:a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:c&lt;/span&gt;
&lt;span class="n"&gt;sortable_fields&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; [:a, :b, :c]&lt;/span&gt;

&lt;span class="n"&gt;sortable_fields&lt;/span&gt; &lt;span class="sx"&gt;%w[d e f]&lt;/span&gt;
&lt;span class="n"&gt;sortable_fields&lt;/span&gt;  &lt;span class="c1"&gt;# =&amp;gt; [:a, :b, :c, "d", "e", "f"]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This method wraps an &lt;code&gt;Array&lt;/code&gt; instead of a &lt;code&gt;Hash&lt;/code&gt;, as you can seen.  With that there's one other trick I sometimes like to throw in that should be called something like "slurp-&lt;code&gt;flatten()&lt;/code&gt;-splat."  By taking any number of arguments, calling &lt;code&gt;flatten()&lt;/code&gt; to avoid worrying about whether the caller used an &lt;code&gt;Array&lt;/code&gt; (say for the convenient syntax used in the examples), and splitting the fields back out, we again get a smooth interface out of minimal code.&lt;/p&gt;

&lt;p&gt;As I said earlier there's no deep magic here, but don't underestimate how nice the simple tricks can be in the right circumstances.  Of course, purists who strongly hold to the "a method should only do one thing" philosophy will need to comfort themselves to sleep by repeating, "It just manages configuration!"&lt;/p&gt;</content>
    <author>
      <name>James Edward Gray II</name>
    </author>
  </entry>
</feed>
