From: Dave Thomas Date: 2002-03-28T12:43:13+09:00 Subject: Re: parsing strings into numbers "Chris Morris" writes: > ... but for any string value, how can I do a proper conversion? I want a > routine like this: > > def convertString(value) > # do magic stuff to convert value to proper Numeric, > # assuming it's a valid Numeric representation > end > > I'd love to be able to call a Ruby routine to do this for me, same as the > parser does when reading in a script. Kinda messy, but you can do result = Integer(value) rescue Float(value) or result = Integer(value) rescue Float(value) rescue nil (depending on whether you want an exception or nil for an invalid number). If you're assuming a valid number, you can also use the evil "eval". Cheers Dave