<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LolocoJr &#187; flog ruby pain</title>
	<atom:link href="http://www.railsguru.com/articles/tag/flog-ruby-pain/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.railsguru.com</link>
	<description>Andy Lo-A-Foe&#039;s blog</description>
	<lastBuildDate>Fri, 09 Jul 2010 12:08:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Torturing Ruby (and laughing at your own code)</title>
		<link>http://www.railsguru.com/articles/2007/08/21/torturing-ruby-and-laughing-at-your-own-code/</link>
		<comments>http://www.railsguru.com/articles/2007/08/21/torturing-ruby-and-laughing-at-your-own-code/#comments</comments>
		<pubDate>Tue, 21 Aug 2007 22:50:00 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[Home]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[flog ruby pain]]></category>

		<guid isPermaLink="false">http://www.railsguru.com/2007/08/21/torturing-ruby-and-laughing-at-your-own-code</guid>
		<description><![CDATA[<p>While cleaning up some code I ran across some obscure code of mine from my Ruby youth. I remembered reading about an ultra cool Ruby tool the other day so I decided to give my code a good flogging. The victim for tonight is a method used to slice ID numbers into chunks of at [...]]]></description>
			<content:encoded><![CDATA[<p>While cleaning up some code I ran across some obscure code of mine from my Ruby youth. I remembered reading about an ultra cool Ruby tool the other day so I decided to <strong><a href="http://ruby.sadi.st/Flog.html">give my code a good flogging</a></strong>. The victim for tonight is a method used to slice ID numbers into chunks of at most 4 characters long. This is required to overcome the typical Linux file system limitation of at most 32000 entries per directory. In this case the project stores roughly a million thumbnail images on disk. We start with the original piece of code:</p>
<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></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">def</span> <span class="fu">splice_number</span>(number, part_size = <span class="i">4</span>)<tt>
</tt>  n = number.to_s<tt>
</tt>  r = []<tt>
</tt>  <span class="r">return</span> r <span class="r">if</span> n.size.zero?<tt>
</tt>  (n.size / part_size).times { |t| r &lt;&lt; n[(t*part_size)..((t<span class="i">+1</span>)*part_size<span class="i">-1</span>)] }<tt>
</tt>  r &lt;&lt; n[-(n.size % part_size)..n.size] <span class="r">if</span> (n.size % part_size) &gt; <span class="i">0</span><tt>
</tt>  r<tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Ah yes, <span class="caps">WTF</span> was I thinking when I wrote this? Who cares, it seemed very clever then! What does Flog think about this?</p>
<pre>
Total score = 28.35

none#splice_number: (28)
     7: size
     3: *
     2: %
     2: []
     2: &lt;&lt;
     1: +
     1: -@
     1: -
     1: /
     1: lit_fixnum
     1: zero?
     1: &gt;
     1: to_s
     1: times
</pre>
<p>Pretty good score. Now it&#8217;s time for some torturing. Say hello to my little friend: unpack!</p>
<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></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">def</span> <span class="fu">splice_number</span>(number, part_size = <span class="i">4</span>)<tt>
</tt>  n = number.to_s<tt>
</tt>  n.unpack(<span class="s"><span class="dl">&quot;</span><span class="k">a</span><span class="il"><span class="dl">#{</span>part_size<span class="dl">}</span></span><span class="dl">&quot;</span></span> * (n.size / part_size) + ((n.size % part_size == <span class="i">0</span>) ? <span class="s"><span class="dl">&quot;</span><span class="dl">&quot;</span></span> : <span class="s"><span class="dl">&quot;</span><span class="k">a*</span><span class="dl">&quot;</span></span>))<tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Flog?</p>
<pre>
Total score = 13.05

none#splice_number: (13)
     3: size
     1: %
     1: /
     1: ==
     1: *
     1: +
     1: to_s
     1: unpack
     0: lit_fixnum
</pre>
<p>Yeow, well over half the pain gone!! Perhaps we can still improve by being less clever?</p>
<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></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">def</span> <span class="fu">splice_number</span>(number, part_size = <span class="i">4</span>)<tt>
</tt>  n = number.to_s<tt>
</tt>  r = n.unpack(<span class="s"><span class="dl">&quot;</span><span class="k">a</span><span class="il"><span class="dl">#{</span>part_size<span class="dl">}</span></span><span class="dl">&quot;</span></span> * (n.size / part_size) + <span class="s"><span class="dl">&quot;</span><span class="k">a*</span><span class="dl">&quot;</span></span>)<tt>
</tt>  r.delete(<span class="s"><span class="dl">&quot;</span><span class="dl">&quot;</span></span>)<tt>
</tt>  r<tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p>More lines, but less code! Hmm?</p>
<pre>
Total score = 9.25

none#splice_number: (9)
     1: size
     1: /
     1: *
     1: +
     1: delete
     1: to_s
     1: unpack
     0: lit_fixnum
</pre>
<p>Weeh, a full 2/3 of the pain flogged out of the code! That&#8217;s all the torture I&#8217;ll do for tonight..</p>
<p>
<strong>Update:</strong> Okay, couldn&#8217;t help myself, last blow:</p>
<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></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }"><span class="r">def</span> <span class="fu">splice_number</span>(number, part_size = <span class="i">4</span>)<tt>
</tt>  n = number.to_s<tt>
</tt>  p = <span class="s"><span class="dl">&quot;</span><span class="k">a</span><span class="il"><span class="dl">#{</span>part_size<span class="dl">}</span></span><span class="dl">&quot;</span></span><tt>
</tt>  t = n.size / part_size<tt>
</tt>  r = n.unpack(p * t + <span class="s"><span class="dl">&quot;</span><span class="k">a*</span><span class="dl">&quot;</span></span>)<tt>
</tt>  r.delete(<span class="s"><span class="dl">&quot;</span><span class="dl">&quot;</span></span>)<tt>
</tt>  r<tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p>With score:</p>
<pre>
Total score = 8.05

none#splice_number: (8)
     1: *
     1: size
     1: +
     1: delete
     1: to_s
     1: /
     1: unpack
     0: lit_fixnum
</pre>
</p>
<p>Done..</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsguru.com/articles/2007/08/21/torturing-ruby-and-laughing-at-your-own-code/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
