[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby equivalent PHP function is_numeric?

Josselin

11/17/2006 4:47:00 PM

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


13 Answers

Paul Lutus

11/17/2006 5:11:00 PM

0

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

Josselin

11/17/2006 5:21:00 PM

0

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

Rimantas Liubertas

11/17/2006 5:27:00 PM

0

> > 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://rim...

Paul Lutus

11/17/2006 5:42:00 PM

0

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

Jason Dusek

11/17/2006 5:58:00 PM

0

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.

> 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

--
_jsn

Ara.T.Howard

11/17/2006 6:15:00 PM

0

Ara.T.Howard

11/17/2006 6:15:00 PM

0

David Vallner

11/18/2006 1:28:00 AM

0

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.

David Vallner

Wilson Bilkovich

11/18/2006 5:21:00 AM

0

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

Michael Fellinger

11/18/2006 9:51:00 AM

0

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

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