In the web application I am writing I expect an optional parameter called
size that is expected to be an integer. I would like to default it to 10 and disregard any bad (non integer input) I came up with this expression:
size = begin env.params.query["size"].to_i rescue 10 end
Is there a better way to write it?
As I was writing this post it occurred to me that size should be a non-negative integer so the above expression is not really good, but I’d still be interested in the answer to the above and maybe to the version where we only accept it when it is a non-negative.