Testing a string's value is a positive integer

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-forum.com/.

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

Farrel

···

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-forum.com/.

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

Hope that helps.

James Edward Gray II

···

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.

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.

···

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

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-forum.com/.

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-forum.com/.

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-forum.com/.

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-forum.com/.

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-forum.com/.

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.

James Gray 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

···

On 29.08.2006 18:57, James Deuchar wrote:

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

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

···

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-forum.com/.
>>>
>> 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 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!

···

On 29/08/06, Floyd Wright <tfwright@thoughtbot.com> wrote:

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