tag:www.rhnh.net,2008:/case Case - Xavier Shay's Blog 2008-05-10T11:37:55Z Enki Xavier Shay notreal@rhnh.net tag:www.rhnh.net,2008:Post/779 2008-05-10T11:37:00Z 2008-05-10T11:37:55Z Hash trumps case <table class="CodeRay"><tr> <td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }"><pre>1<tt> </tt>2<tt> </tt>3<tt> </tt>4<tt> </tt>5<tt> </tt>6<tt> </tt>7<tt> </tt>8<tt> </tt>9<tt> </tt><strong>10</strong><tt> </tt>11<tt> </tt>12<tt> </tt>13<tt> </tt>14<tt> </tt>15<tt> </tt>16<tt> </tt>17<tt> </tt></pre></td> <td class="code"><pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="c"># Two equivalent functions</span><tt> </tt><span class="r">def</span> <span class="fu">rgb</span>(color)<tt> </tt> <span class="r">case</span> color<tt> </tt> <span class="r">when</span> <span class="sy">:red</span> <span class="r">then</span> <span class="s"><span class="dl">'</span><span class="k">ff0000</span><span class="dl">'</span></span><tt> </tt> <span class="r">when</span> <span class="sy">:green</span> <span class="r">then</span> <span class="s"><span class="dl">'</span><span class="k">00ff00</span><span class="dl">'</span></span><tt> </tt> <span class="r">when</span> <span class="sy">:blue</span> <span class="r">then</span> <span class="s"><span class="dl">'</span><span class="k">0000ff</span><span class="dl">'</span></span><tt> </tt> <span class="r">else</span> <span class="s"><span class="dl">'</span><span class="k">000000</span><span class="dl">'</span></span> <span class="c"># Default to black</span><tt> </tt> <span class="r">end</span><tt> </tt><span class="r">end</span><tt> </tt><tt> </tt><span class="r">def</span> <span class="fu">rgb2</span>(color)<tt> </tt> {<tt> </tt> <span class="sy">:red</span> =&gt; <span class="s"><span class="dl">'</span><span class="k">ff0000</span><span class="dl">'</span></span>,<tt> </tt> <span class="sy">:green</span> =&gt; <span class="s"><span class="dl">'</span><span class="k">00ff00</span><span class="dl">'</span></span>,<tt> </tt> <span class="sy">:blue</span> =&gt; <span class="s"><span class="dl">'</span><span class="k">0000ff</span><span class="dl">'</span></span><tt> </tt> }[color] || <span class="s"><span class="dl">'</span><span class="k">000000</span><span class="dl">'</span></span><tt> </tt><span class="r">end</span><tt> </tt></pre></td> </tr></table> <p>Even though these functions are equivalent, the second carries more semantic weight &#8211; it maps a symbol directly to a color. The <code>case</code> sample makes no such guarantees since you can execute any arbitrary code in the <code>then</code> block. In addition, a hash is easier to work with &#8211; you can easily iterate over the keys, extract to another method if you need reuse, or query it for other properties (for example, 3 colors are available). It is also easier to read &#8211; both aesthetically and because it contains fewer tokens. In almost all circumstances I will prefer a hash over a case statement.</p> <p><strong>Relationships in data are easier to comprehend and manipulate than relationships in code.</strong></p>