ruby equivalent PHP function is_numeric?

J

Josselin

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
 
J

Josselin

------------------------------------
#!/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.

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

Rimantas Liubertas

After reading completely my Ruby book, I cannot find a function
def is_numeric?(s)
return (s.to_s =~ /^\d+(\.\d+|\d*)$/)?true:false
end
=> false

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

Regards,
Rimantas
 
J

Jason Dusek

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
 
A

ara.t.howard

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

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
 
A

ara.t.howard

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
 
D

David Vallner

--------------enig27CCDCE399C256A2165E8F0F
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Sat, 18 Nov 2006, Josselin wrote:
=20
=20
=20
=20
harp:~ > cat a.rb
def is_numeric?(n) Float n rescue false end
=20

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


--------------enig27CCDCE399C256A2165E8F0F
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFXmF/y6MhrS8astoRAq76AJwO3sX6PW3W4A1CKVUD23fC0+LutgCfds7P
/JrGEKQoWb0FKau2kPZGFis=
=BWng
-----END PGP SIGNATURE-----

--------------enig27CCDCE399C256A2165E8F0F--
 
W

Wilson Bilkovich

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
 
M

Michael Fellinger

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
 
M

matt neuburg

Michael Fellinger said:
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.
 
T

Timothy Hunter

matt said:
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."
 
D

David Vallner

--------------enigE291B1F6ACAB3C0E7941C2E1
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Timothy said:
Pickaxe 2nd ed. page 374: "Exceptions may be handled...after the
execution of a single statement."
=20
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 ;)

David Vallner


--------------enigE291B1F6ACAB3C0E7941C2E1
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFYJb8y6MhrS8astoRAk/CAJ9jIJKzw6OCp3mWKFwnyy8a4nWFhACePBVQ
VS5x5t6qFs+9o0dodfOIxEc=
=yI35
-----END PGP SIGNATURE-----

--------------enigE291B1F6ACAB3C0E7941C2E1--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top