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

On 29/08/06, James D. [email protected] 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

Farrel L. wrote:

On 29/08/06, James D. [email protected] 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.

On 29/08/06, Floyd W. [email protected] wrote:

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

On Aug 29, 2006, at 11:00 AM, James D. 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 G. II

Farrel L. wrote:

On 29/08/06, Floyd W. [email protected] 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!

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

On 29.08.2006 18:57, James D. wrote:

James Edward G. 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

James G. wrote:

On Aug 29, 2006, at 11:00 AM, James D. 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 G. II

Thanks James - it does indeed

On Aug 29, 2006, at 12:00 PM, James D. 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.