From: "David A. Black" Date: 2007-10-26T01:56:04+09:00 Subject: Re: Change a string to an integer, report an error if the string does not represent an integer? Hi -- On Fri, 26 Oct 2007, Randy Kramer wrote: > Can anybody point me to a way to check if a string represents a valid integer > and then convert it to an integer? > > It is very likely the string will contain leading zeros, and should not > contain any trailing non-numeric characters. > > Per the documentation (pickaxe 2), to_i won't quite do it, it will just ignore > trailing non-numeric characters. There's a method called Integer (uppercase I and all). It blows up if the string has extra stuff: irb(main):001:0> Integer("0003") => 3 irb(main):002:0> Integer("0003a") ArgumentError: invalid value for Integer: "0003a" David -- Upcoming training by David A. Black/Ruby Power and Light, LLC: * Advancing With Rails, Edison, NJ, November 6-9 * Advancing With Rails, Berlin, Germany, November 19-22 * Intro to Rails, London, UK, December 3-6 (by Skills Matter) See http://www.rubypal.com for details!