<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Rant first, think later. comments on Injured by Integer</title>
    <link>http://www.nicholaswright.org/blog/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Rant first, think later. comments</description>
    <item>
      <title>"Injured by Integer" by abica</title>
      <description>&lt;p&gt;It&amp;#8217;s been a very long time since I&amp;#8217;ve used &lt;a href="http://www.ruby-doc.org/core/classes/Kernel.html#M005986"&gt;Kernel#Integer&lt;/a&gt;, but to my recollection it was simply coercion that raised an error if necessary, as an alternative to things like &lt;a href="http://www.ruby-doc.org/core/classes/String.html#M000802"&gt;String#to_i&lt;/a&gt; which would simply return the first digits in a string, or 0 if the string did not contain a valid integer.&lt;/p&gt;


	&lt;p&gt;Since the program I was writing was receiving amounts in the form of strings representing cents, I figured it would be a natural fit. Leading zeros would be stripped off of anything under a buck, and I could simply rescue any ArgumentError and handle them appropriately.&lt;/p&gt;


	&lt;p&gt;A short while in production and I&amp;#8217;m starting to get some bug reports that are pretty tricky to replicate.&lt;/p&gt;


	&lt;p&gt;Turns out, my memory has failed me yet again! Integer does not, in fact, do anything close to what I thought it did.&lt;/p&gt;


	&lt;p&gt;Let&amp;#8217;s take a peek inside irb so we can take a harder look at what was causing all of my problems.&lt;/p&gt;


&lt;pre&gt;
&amp;gt;&amp;gt; Integer( "085" )
ArgumentError: invalid value for Integer: "085" 
    from (irb):3:in `Integer'
    from (irb):3
&amp;gt;&amp;gt; Integer( "022" )
=&amp;gt; 18
&lt;/pre&gt;

	&lt;p&gt;Now at a glance you can probably already deduce that I was expecting to get 85 and 22 as the respective results, not an ArgumentError and 18! Keep in mind that I also had a bit of special sauce to handle ArgumentErrors, I just hadn&amp;#8217;t planned on arriving at them through the avenue shown above.&lt;/p&gt;


	&lt;p&gt;A quick peek at the &lt;a href="http://www.ruby-doc.org/core/classes/Kernel.html#M005986"&gt;&lt;span class="caps"&gt;API&lt;/span&gt;&lt;/a&gt; tells me exactly why such behavior was occurring:&lt;/p&gt;


	&lt;p&gt;&amp;#8220;If &lt;em&gt;arg&lt;/em&gt; is a String, leading radix indicators (0, 0b, and 0x) are honored. Others are converted using to_int and to_i.&amp;#8221;&lt;/p&gt;


	&lt;p&gt;Wow. Ok. More than a little unexpected. After having discovered the problem, the bug it caused was easily squashed. Lesson learned, programming from memory is not always a great idea. Even when the tests pass!&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;NOTE TO SELF&lt;/span&gt;:&lt;/strong&gt; Kernel#Integer is &lt;em&gt;not&lt;/em&gt; simply an alternative to String#to_i that raises an error.&lt;/p&gt;


	&lt;p&gt;You live, you learn. And then you get luvz.&lt;/p&gt;

</description>
      <pubDate>Fri,  4 Apr 2008 21:35:00 CDT</pubDate>
      <guid>&lt;a href="/blog/articles/2008/04/04/injured-by-integer"&gt;Injured by Integer&lt;/a&gt;</guid>
      <link>&lt;a href="/blog/articles/2008/04/04/injured-by-integer"&gt;Injured by Integer&lt;/a&gt;</link>
    </item>
  </channel>
</rss>
