Michael Fellinger
11/18/2006 9:51:00 AM
On 11/18/06, Wilson Bilkovich <wilsonb@gmail.com> wrote:
> On 11/17/06, David Vallner <david@vallner.net> wrote:
> > ara.t.howard@noaa.gov wrote:
> > > On Sat, 18 Nov 2006, Josselin wrote:
> > >
> > >> After reading completely my Ruby book, I cannot find a function
> > >> equivalent to the PHP is_numeric? to test a String
> > >>
> > >> myString.is_numeric? (check only digits and dot character) =>
> > >> return true or false
> > >>
> > >> does it exist ? or it's more complicated than that ? (need to build a
> > >> regex... ?)
> > >>
> > >> thanks
> > >>
> > >> joss
> > >
> > >
> > >
> > > harp:~ > cat a.rb
> > > def is_numeric?(n) Float n rescue false end
> > >
> >
> > def is_numeric?(n) begin Float n; return true rescue false end
> >
> > Predicate methods not returning a boolean make my skin crawl even if
> > it's all the same difference for conditionals.
> >
>
> Or the golf version:
> def is_numeric?(n)
> !!Float(n) rescue false
> end
and the handy version, if you actually don't care but want something
nice instead:
class String
def to_numeric
Integer(self) rescue Float(self) rescue nil
end
end