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!
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”.
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.