From: Michael Fellinger Date: 2006-11-18T18:50:34+09:00 Subject: Re: ruby equivalent PHP function is_numeric? On 11/18/06, Wilson Bilkovich wrote: > On 11/17/06, David Vallner 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