Robert Klemme
8/29/2006 5:35:00 PM
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