Ruby equivalent PHP function is_numeric?

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

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... ?)

------------------------------------
#!/usr/bin/ruby -w

def is_numeric?(s)
  return (s.to_s =~ /^\d+(\.\d+|\d*)$/)?true:false
end

puts is_numeric?(1.2345)
puts is_numeric?(12345678987654321)
puts is_numeric?(0)
puts is_numeric?(0.0)
puts is_numeric?(".001")
puts is_numeric?(123435.12345)
puts is_numeric?("123435.")
------------------------------------

Output:

true
true
true
true
false
true
false

An extension to this function would handle float exponents and associated
characters.

--
Paul Lutus
http://www.arachnoid.com

After reading completely my Ruby book, I cannot find a function
equivalent to the PHP is_numeric? to test a String

There is no such function, although you can pretty readily use regexes
to do it...

It really is too bad that String.to_f does not throw an exception - or
at least return NaN - when the string is not a number.

myString.is_numeric? (check only digits and dot character)

A regex that matches the cases where String.to_f will find a value is
simple to write, and you can create String.numeric? like so:

class String
  def numeric?
    not ( self =~ /^[[:digit:]]+(\.[[:digit:]]+)?([eE][[:digit:]]+)?/ or
          self =~ /^\.[[:digit:]]+([eE][[:digit:]]+)?/ ).nil?
  end
end

···

On 11/17/06, Josselin <josselin@wanadoo.fr> wrote:

--
_jsn

harp:~ > cat a.rb
   def is_numeric?(n) Float n rescue false end

   args =
     1.2345,
     12345678987654321,
     0,
     0.0,
     ".001",
     123435.12345,
     "123435.",
     "1.50130937545297e+68",
     "a",
     "a42",
     "42a",
     1_2_3.42,
     "1_2_3.42",
     "_1_2_3_.42_",
     "__1__2_3_.42__"

   args.each{|a| puts "is_numeric?(#{ a.inspect }) #=> #{ is_numeric? a }"}

   harp:~ > ruby a.rb
   is_numeric?(1.2345) #=> 1.2345
   is_numeric?(12345678987654321) #=> 1.23456789876543e+16
   is_numeric?(0) #=> 0.0
   is_numeric?(0.0) #=> 0.0
   is_numeric?(".001") #=> 0.001
   is_numeric?(123435.12345) #=> 123435.12345
   is_numeric?("123435.") #=> 123435.0
   is_numeric?("1.50130937545297e+68") #=> 1.50130937545297e+68
   is_numeric?("a") #=> false
   is_numeric?("a42") #=> false
   is_numeric?("42a") #=> false
   is_numeric?(123.42) #=> 123.42
   is_numeric?("1_2_3.42") #=> 123.42
   is_numeric?("_1_2_3_.42_") #=> false
   is_numeric?("__1__2_3_.42__") #=> false

-a

···

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

--
my religion is very simple. my religion is kindness. -- the dalai lama

thanks Paul... I see why there is no such function (I am translating PHP -> Ruby) , too many cases
better using a regexp....

···

On 2006-11-17 18:11:10 +0100, Paul Lutus <nospam@nosite.zzz> said:

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... ?)

------------------------------------
#!/usr/bin/ruby -w

def is_numeric?(s)
  return (s.to_s =~ /^\d+(\.\d+|\d*)$/)?true:false
end

puts is_numeric?(1.2345)
puts is_numeric?(12345678987654321)
puts is_numeric?(0)
puts is_numeric?(0.0)
puts is_numeric?(".001")
puts is_numeric?(123435.12345)
puts is_numeric?("123435.")
------------------------------------

Output:

true
false
true
false

An extension to this function would handle float exponents and associated
characters.

> 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

<...>

def is_numeric?(s)
  return (s.to_s =~ /^\d+(\.\d+|\d*)$/)?true:false
end

<...>

is_numeric?("1000_000")

=> false

In ruby it is not only about dot and digits, but also underscores.

Regards,
Rimantas

···

--
http://rimantas.com/

use

   Float(a_string)

it does throw.

-a

···

On Sat, 18 Nov 2006, Jason Dusek wrote:

On 11/17/06, Josselin <josselin@wanadoo.fr> wrote:

After reading completely my Ruby book, I cannot find a function
equivalent to the PHP is_numeric? to test a String

There is no such function, although you can pretty readily use regexes
to do it...

It really is too bad that String.to_f does not throw an exception - or
at least return NaN - when the string is not a number.

--
my religion is very simple. my religion is kindness. -- the dalai lama

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.

David Vallner

···

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

Rimantas Liubertas 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

<...>

def is_numeric?(s)
  return (s.to_s =~ /^\d+(\.\d+|\d*)$/)?true:false
end

<...>

is_numeric?("1000_000")

=> false

In ruby it is not only about dot and digits, but also underscores.

What purpose does the underscore play? I see that it is accepted, I just
can't figure out why. I have never seen it as part of proper numeric
syntax, anywhere, in three decades in this business.

a = 1_2_3_4_5_6_7 # => 1234567

What's the point?

···

--
Paul Lutus
http://www.arachnoid.com

Or the golf version:
def is_numeric?(n)
  !!Float(n) rescue false
end

···

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.

What purpose does the underscore play? I see that it is accepted, I just

can't figure out why. I have never seen it as part of proper numeric
syntax, anywhere, in three decades in this business.

It's used to make numbers easier to read:

1_000_000_000 vs. 1000000000

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

···

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

Where can I find documentation on the syntax of "rescue" that makes this
work?

Thx - m.

···

Michael Fellinger <m.fellinger@gmail.com> wrote:

class String
  def to_numeric
    Integer(self) rescue Float(self) rescue nil
  end
end

--
matt neuburg, phd = matt@tidbits.com, Matt Neuburg’s Home Page
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

matt neuburg wrote:

···

Michael Fellinger <m.fellinger@gmail.com> wrote:

class String
  def to_numeric
    Integer(self) rescue Float(self) rescue nil
  end
end
    
Where can I find documentation on the syntax of "rescue" that makes this
work?

Thx - m.

Pickaxe 2nd ed. page 374: "Exceptions may be handled...after the execution of a single statement."

Timothy Hunter wrote:

matt neuburg wrote:

class String
  def to_numeric
    Integer(self) rescue Float(self) rescue nil
  end
end
    
Where can I find documentation on the syntax of "rescue" that makes this
work?

Thx - m.

Pickaxe 2nd ed. page 374: "Exceptions may be handled...after the
execution of a single statement."

It's basically a hack around the fact that as "if", "while", etc. are
available as both block forms and statement modifiers, "rescue" is only
a statement modifier.

There is no "syntax of rescue" that makes this work, as this is in fact
the only syntax of rescue :wink:

David Vallner

···

Michael Fellinger <m.fellinger@gmail.com> wrote: