Pitfalls with Ruby’s Integer-Constructor

30 Jul
2009

Got an strange error:

 invalid value for Integer "09"

What did I do? (…)Integer(somestring)(…) where somestring was something between 01-99.
Turns out that an leading zero tells ruby that this is octal. Hell, I don’t have any octal stuff in my web app.

Solution: Do it the ruby way, use .to_i – even cleaner, and no octal nonsense ;)

(Why I posted this? Because I never got that error, only after hitting production mode, someone committed a 09. Pretty everything else is parseable, 00-07 is valid octal)

Here is the source.

Related posts:

  1. Demystifiying Rails Model attributes overriding
  2. HowTo update rake 0.8.1 -> 0.8.4 on debian

Comment Form

top

Switch to our mobile site