well... you can just do:

class String
  def is_numeric?
    !self.to_f.nil?
  end
end


- Matt


On Fri, Jan 22, 2010 at 12:18 PM, Greg Willits <[email protected]> wrote:

> On Jan 22, 2010, at 11:35 AM, Glenn Little wrote:
>
>  I'm looking for a simple way to check if a string really represents
>> a number in ruby/rails.  I figured there would be a String.is_numeric?
>> but haven't found anything.
>>
>
> I have independent interpretations of is_numeric and represents_number.
>
> "123.6 cm" and "$19.95" are not numeric, but they do represent a number.
>
> "-123.45" is numeric and represents a number.
>
> "|123.45|" is numeric and represents a number.
>
> "1.55e08" is numeric and represents a number.
>
> "1.55e-08" is numeric and represents a number.
>
> For Rails, you might be interested in my custom AR validations...
> http://www.gregwillits.ws/articles/custom-validation-in-rails
>
> Though these are designed to specifically isolate what type of number I
> want (integer, decimal, etc) in a form input field, so might not be what
> you're after as a general purpose method.
>
> Personally, I prefer the regex route. It'll take a little work to think
> about exactly what I'm willing to accept, but at least I know more
> explicitly how to predict and test the behavior.
>
> --
> def gw
>  lives_at 'www.gregwillits.ws'
> end
>
>
> --
> SD Ruby mailing list
> [email protected]
> http://groups.google.com/group/sdruby
>

-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby

Reply via email to