DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Fred has posted 10 posts at DZone. View Full User Profile

Determine If A Syntactically Correct Series Of Characters Is Numeric (the Golf Version) In Ruby

  • submit to reddit
        Ruby Forum
Re: ruby equivalent PHP function is_numeric?
Posted by Wilson Bilkovich (Guest) on 18.11.2006 06:22  
On 11/17/06, David Vallner <> wrote:
> >> regex... ?)
> 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

def is_numeric?(n) !!Float(n) rescue false end
def is_numeric?(n) {!!Float(n) rescue false}