Daniel Finnie
3/12/2007 12:19:00 AM
I think this depends somewhat on your definition of an argument.
If I do "0101110".to_i(2), then clearly 2 is an argument. But is
"0101110"? Technically, no, it is the receiver. In Ruby's duck typed
world where multiple, same named methods do different things depending
on class, then it kindof is an argument.
Dan
Gary Wright wrote:
>
> On Mar 11, 2007, at 6:39 PM, 7stud 7stud wrote:
>
>> Eden Li wrote:
>>> irb(main):036:0> Class.constants.map { |c| eval(c).new rescue
>>> nil }.compact.map { |s| Integer(s) rescue $! }.select { |c| c.is_a?
>>> (Exception) }.map { |c| c.class }.uniq
>>> => [TypeError, ArgumentError, NoMethodError]
>>>
>>> Looks like it could TypeError and NoMethodError as well...
>>
>> So the docs don't "document" which exceptions can be thrown by a method?
>
> The documentation isn't perfect. There is information about contributing
> to Ruby documentation at www.ruby-doc.org.
>
> It has been my observation that the Ruby style is to avoid the use
> of exceptions as part of the expected behavior of a method. What I mean
> is that, in general, if you provide arguments to a method that meet the
> method's pre-conditions then you will get back a result with no exception
> being raised. If you provide arguments that *don't* meet the
> pre-conditions of a method, well then you may or may not get an exception
> but it is really your problem in either case because you violated the
> pre-condition and all bets are off.
>
> Test driven development really puts the burden on the caller to provide
> the correct arguments and methods are often written with that explicit
> expectation, which tends to reduce the amount of argument checking that
> is done in methods.
>
> When exceptions are explicitly part of the behavior of a method then I,
> of course, think their use should be documented.
>
> I'd be interested in other perspectives on best practices with respect
> to exceptions and argument checking in API/class design.
>
>
> Gary Wright
>
>
>
>
>