From: Farrel Lifson Date: 2006-08-30T01:10:54+09:00 Subject: Re: Testing a string's value is a positive integer On 29/08/06, James Deuchar wrote: > Hi, > > I'm trying to find an nice way of verifying that the value of a String > object (from the RoR params) contains a positive integer value. > > Casting my string to_i and then testing that it is_a(Numeric) and is > 0 > doesn't do the trick because the to_i method converts something like > '1.1' to a Fixnum of value 1 which would then pass the test when it > shouldn't... > > I feel certain I must be missing a trick here...am about to go down the > path of regexp which feels...wrong? > > Any advice for a newcomer would be gratefully received! > > Thanks > > James > > > > -- > Posted via http://www.ruby-forum.com/. > > Check if num.to_f - num.to_i == 0, if that's true you're dealing with a integer otherwise a float. Farrel