From: "Marcel Molina Jr." Date: 2007-06-21T07:27:20+09:00 Subject: Re: Code Critique: check if int or float On Thu, Jun 21, 2007 at 07:17:53AM +0900, ole __ wrote: > I'm really starting to like this language. I'm working towards building > a simple expression evaluator. I couldn't seem to find any methods for > testing whether a string contains an integer or float. In PHP (where I'm > from) you've got your is_numeric(). Is there such a method in Ruby? > > Anyway, under the assumption that there wasn't and to learn a bit I > decided to add the methods i? and f? to String. I was wondering if I > could get a bit of a critique on this code because I'm still ultra new > to Ruby. >> '3s'.to_i => 3 >> Integer('3s') ArgumentError: invalid value for Integer: "3s" from (irb):3:in `Integer' from (irb):3 >> '3s'.to_f => 3.0 >> Float('3s') ArgumentError: invalid value for Float(): "3s" from (irb):5:in `Float' from (irb):5 >> '-8e3'.to_f => -8000.0 >> Float('-8e3') => -8000.0 >> '-8e3'.to_i => -8 >> Integer('-8e3') ArgumentError: invalid value for Integer: "-8e3" from (irb):9:in `Integer' from (irb):9 You could likely come up with a much cleaner implementation using Integer() and Float() + rescue ArgumentError. And about camelCase, it's true, almost no one in Ruby uses camelCase. I'd recommend just going with the flow and adoption lowercase_and_underscored. marcel -- Marcel Molina Jr.