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.

On 2006-11-17 18:11:10 +0100, Paul L. [email protected] said:

puts is_numeric?(0.0)
true
false
true
false

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

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

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

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

On 11/17/06, Josselin [email protected] 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.

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

Rimantas L. wrote:

<…>

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?

On Sat, 18 Nov 2006, Josselin wrote:

joss

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, Jason Dusek wrote:

On 11/17/06, Josselin [email protected] 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.

use

Float(a_string)

it does throw.

-a

[email protected] wrote:

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.

David V.

On 11/17/06, David V. [email protected] 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
end

Michael F. [email protected] 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.

On 11/18/06, Wilson B. [email protected] wrote:

does it exist ? or it’s more complicated than that ? (need to build a

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

Timothy H. wrote:

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 V.

matt neuburg wrote:

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.”