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