On Aug 21, 2009, at 04:36, Jes=FAs Gabriel y Gal=E1n wrote:
>> Someone using the method calculate_bounding_box() has no way of =20
>> knowing that
>> you're calling "ceil" internally, so getting an error back saying
>> "NoMethodError: undefined method `ceil' for "2343":String" will be
>> confusing.
>
> He will know when he sees that error, if it wasn't in the
> documentation of the method.

Right, and will sit there confused saying "What?  What does 'ceil' =20
have to do with anything?" as opposed to "Oh, that method expects =20
numeric arguments."

>> If you are nice enough to catch that error and throw a more useful =20=

>> error:
>> "ArgumentError: width and height must be numeric" it will make =20
>> debugging
>> much easier.
>
> The duck typing philosophy says that the argument need not be Numeric,
> only respond to "ceil" :-)
> So if you change ArgumentError to say: "#{x} should respond to :ceil",
> isn't that pretty similar to the NoMethodError?

Duck typing is why you catch the exception from 'ceil' rather than do =20=

this:

# don't do this
if (not weight.class =3D=3D Numeric) or (not height.class =3D=3D =
Numeric)
   raise ArgumentError, "weight and height must be numeric"
end

If someone passes in an argument that "quacks like a Numeric" and =20
responds to ceil, they won't get an error message about invalid =20
arguments because no exception will be thrown.

Saying "should respond to `ceil'" is misleading anyhow.  You don't =20
just want the arguments to respond to the method call, you want them =20
to do it in a way that's equivalent to how a Numeric would respond -- =20=

i.e. returns an integer greater-than or equal to the argument.

You could make the error message: ArgumentError, =20
"calculate_bounding_box() calls `ceil' on `width' and `height' to =20
create two bounding integers, which are then multiplied together.  =20
Ensure that any arguments you pass in are either Numeric, or respond =20
to `ceil' in the same way a numeric would.", but for a 7-line method, =20=

that's really overkill.

Ben