On Thu, Mar 13, 2014 at 10:35 PM, [email protected] wrote:
No. There is already one standard solution. Best is to just use
Integer and handle the exception.
size = Integer(gets)
“Best” is a matter of taste…
I’d say it’s a matter of values: your values determine what you
consider better or worse. But of course there is a certain factor of
subjectivity here. OTOH there are best practices in the industry.
I prefer using a regex instead of raising and handling an exception,
Fact remains that you are duplicating the parsing code. If at some
point in time what Integer() accepts as valid input changes (not very
likely in this case) you have diverging implementations. Redundancy
is usually bad. Maybe it’s a bit more obvious with floating point
numbers as the regexp to parse that is significantly more complex. You
don’t really want
since I consider wrong user input not to be exceptional.
Well, nobody said you need to let the exception escape the user input
routine. Advantage of using exceptions is that you only have one
operation (the conversion to int) and proceed normally if it succeeds;
if it fails you take another code path.
Usually it’s better to execute an operation and deal with failure than
to try to determine whether it will work, execute it - and still have
to deal with failure. This is especially true for situations where
conditions can change between the check and the operation which allow
the operation still to fail. Typical example is: you check network
connectivity (e.g. to a database) and then do the actual connection.
You always have to deal with potential connection failures, even if
the check succeeded. So you are making the code more complicated and
do not gain anything.
This would not raise any exception, but you certainly would
want it to raise one (NameError).
That will be apparent quite soon during testing. You’ll also get
another exception when using the object as an integer.
$ ruby -e ‘2 + nil’
+': nil can't be coerced into Fixnum (TypeError) from -e:1:in’
$ ruby -e ‘nil + 2’
<main>': undefined method+’ for nil:NilClass (NoMethodError)