[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Testing a string's value is a positive integer

James Deuchar

8/29/2006 4:01:00 PM

Hi,

I'm trying to find an nice way of verifying that the value of a String
object (from the RoR params) contains a positive integer value.

Casting my string to_i and then testing that it is_a(Numeric) and is > 0
doesn't do the trick because the to_i method converts something like
'1.1' to a Fixnum of value 1 which would then pass the test when it
shouldn't...

I feel certain I must be missing a trick here...am about to go down the
path of regexp which feels...wrong?

Any advice for a newcomer would be gratefully received!

Thanks

James



--
Posted via http://www.ruby-....

9 Answers

Farrel Lifson

8/29/2006 4:11:00 PM

0

On 29/08/06, James Deuchar <jamesdeuchar@hotmail.com> wrote:
> Hi,
>
> I'm trying to find an nice way of verifying that the value of a String
> object (from the RoR params) contains a positive integer value.
>
> Casting my string to_i and then testing that it is_a(Numeric) and is > 0
> doesn't do the trick because the to_i method converts something like
> '1.1' to a Fixnum of value 1 which would then pass the test when it
> shouldn't...
>
> I feel certain I must be missing a trick here...am about to go down the
> path of regexp which feels...wrong?
>
> Any advice for a newcomer would be gratefully received!
>
> Thanks
>
> James
>
>
>
> --
> Posted via http://www.ruby-....
>
>

Check if num.to_f - num.to_i == 0, if that's true you're dealing with
a integer otherwise a float.

Farrel

William Crawford

8/29/2006 4:20:00 PM

0

Farrel Lifson wrote:
> On 29/08/06, James Deuchar <jamesdeuchar@hotmail.com> wrote:
>> I feel certain I must be missing a trick here...am about to go down the
>> --
>> Posted via http://www.ruby-....
>>
>>
>
> Check if num.to_f - num.to_i == 0, if that's true you're dealing with
> a integer otherwise a float.
>
> Farrel

num.to_f == num.to_i

Would also give true/false on whether or not it's an integer or not.

--
Posted via http://www.ruby-....

Floyd Wright

8/29/2006 4:27:00 PM

0

irb(main):005:0> 1.5.integer?
=> false

William Crawford wrote:
> Farrel Lifson wrote:
>> On 29/08/06, James Deuchar <jamesdeuchar@hotmail.com> wrote:
>>> I feel certain I must be missing a trick here...am about to go down the
>>> --
>>> Posted via http://www.ruby-....
>>>
>>>
>> Check if num.to_f - num.to_i == 0, if that's true you're dealing with
>> a integer otherwise a float.
>>
>> Farrel
>
> num.to_f == num.to_i
>
> Would also give true/false on whether or not it's an integer or not.
>


Farrel Lifson

8/29/2006 4:36:00 PM

0

On 29/08/06, Floyd Wright <tfwright@thoughtbot.com> wrote:
> irb(main):005:0> 1.5.integer?
> => false
>
> William Crawford wrote:
> > Farrel Lifson wrote:
> >> On 29/08/06, James Deuchar <jamesdeuchar@hotmail.com> wrote:
> >>> I feel certain I must be missing a trick here...am about to go down the
> >>> --
> >>> Posted via http://www.ruby-....
> >>>
> >>>
> >> Check if num.to_f - num.to_i == 0, if that's true you're dealing with
> >> a integer otherwise a float.
> >>
> >> Farrel
> >
> > num.to_f == num.to_i
> >
> > Would also give true/false on whether or not it's an integer or not.
> >
>
>
>

Or you could just use that. Three cheers for the principle of least suprise!

James Gray

8/29/2006 4:45:00 PM

0

On Aug 29, 2006, at 11:00 AM, James Deuchar wrote:

> Hi,
>
> I'm trying to find an nice way of verifying that the value of a String
> object (from the RoR params) contains a positive integer value.

str =~ /\A\+?0*[1-9]\d*\Z/

Hope that helps.

James Edward Gray II


James Deuchar

8/29/2006 4:52:00 PM

0

Farrel Lifson wrote:
> On 29/08/06, Floyd Wright <tfwright@thoughtbot.com> wrote:
>> >>>
>>
>>
>
> Or you could just use that. Three cheers for the principle of least
> suprise!

Yep - would be great! However I'm dealing with a params key-value pair
which RoR makes available a String object...

irb(main):1:0> a = "1.1"
=> "1.1"
irb(main):2:0> a.integer?
NoMethodError: undefined method `integer?' for "1.1":String

Maybe regexp ain't so wrong after-all...

Thanks anyways!

--
Posted via http://www.ruby-....

James Deuchar

8/29/2006 4:58:00 PM

0

James Gray wrote:
> On Aug 29, 2006, at 11:00 AM, James Deuchar wrote:
>
>> Hi,
>>
>> I'm trying to find an nice way of verifying that the value of a String
>> object (from the RoR params) contains a positive integer value.
>
> str =~ /\A\+?0*[1-9]\d*\Z/
>
> Hope that helps.
>
> James Edward Gray II

Thanks James - it does indeed

--
Posted via http://www.ruby-....

Robert Klemme

8/29/2006 5:35:00 PM

0

On 29.08.2006 18:57, James Deuchar wrote:
> James Gray wrote:
>> On Aug 29, 2006, at 11:00 AM, James Deuchar wrote:
>>
>>> Hi,
>>>
>>> I'm trying to find an nice way of verifying that the value of a String
>>> object (from the RoR params) contains a positive integer value.
>> str =~ /\A\+?0*[1-9]\d*\Z/
>>
>> Hope that helps.
>>
>> James Edward Gray II
>
> Thanks James - it does indeed

How about

>> Integer("100000000000000000000000") > 0 rescue false
=> true
>> Integer("-100000000000000000000000") > 0 rescue false
=> false
>> Integer("-100000000000000000000000.4") > 0 rescue false
=> false
>> Integer("100000000000000000000000.4") > 0 rescue false
=> false

IOW, if it correctly converts to an int it's compared to 0 (even works
for Bignum) and if not an exception is thrown and the "rescue" clause
turns that into "false".

Kind regards

robert

Logan Capaldo

8/29/2006 9:13:00 PM

0


On Aug 29, 2006, at 12:00 PM, James Deuchar wrote:

> Hi,
>
> I'm trying to find an nice way of verifying that the value of a String
> object (from the RoR params) contains a positive integer value.
>

Isn't this a job for validates_numericality_of ?

validates_numericality_of :value, :only_integer => true
Ok, it doesn't check for postiveness as far as I can tell but, thats
just value > 0 given that you know its an integer.

> Casting my string to_i and then testing that it is_a(Numeric) and
> is > 0
> doesn't do the trick because the to_i method converts something like
> '1.1' to a Fixnum of value 1 which would then pass the test when it
> shouldn't...
>
> I feel certain I must be missing a trick here...am about to go down
> the
> path of regexp which feels...wrong?
>
> Any advice for a newcomer would be gratefully received!
>
> Thanks
>
> James
>
>
>
> --
> Posted via http://www.ruby-....
>