From: Mike Stok Date: 2007-08-27T05:38:27+09:00 Subject: Re: Strict conversion of strings to ints? On 26-Aug-07, at 4:32 PM, James Edward Gray II wrote: > On Aug 26, 2007, at 3:26 PM, Kenneth McDonald wrote: > >> The real problem is: >> >> irb(main):002:0> "12,345".to_i >> => 12 >> >> Basically, Ruby converts anything that starts with an int, which >> is really quite nasty in a case such as the above. I spent a lot >> of time tracking down that problem. >> >> So, I want an integer conversion function that throws an error if >> the _entire_ string can't be interpreted as an integer; the Ruby >> analog to the standard string-to-int conversion function in most >> other languages. > > raise "Bad integer" unless i_str =~ /\A-?\d+\z/ What about int = Integer(i_str) ? It accepts leading whitespace, but apart from that it's pretty picky. Mike -- Mike Stok http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.