On Thu, 6 Dec 2007, Trans wrote:

> Special methods? Just do:
>
>    def initialize( _name, _drawable, _theme)
>      raise ArgumentError unless _name.instance_of?(String)
>      raise ArgumentError unless _drawable.kind_of?(Drawable)
>      raise ArgumentError unless [:border_color, :fill_color].all?{|m|
> _theme.respond_to?(m)}
>      @name, @drawable, @theme = _name, _drawable, _theme
>    end

Sure you can. However for more typing you get a lower quality / less
informative, less "says what I mean" result.

> Besides, with exception to the 2nd (done for the right reason) this is
> all generally considered a bad practice.

Maybe. But wow it's really nifty for finding a large class of bugs
that otherwise would be pretty hard to find.

> But the idea behind duck-typing is to NOT do these types of checks.

Sometimes duck typing makes sense. If in some distant future I reuse a
chunk of functionality in a duckable sense. In that distant future
changing that line of code (I can instantly see which line from the
backtrace) to a ".quacks_like" is trivial.

However, for every time I have reused code in a duckable sense, I have
caught several bugs with these precondition asserts. That's a really
good trade off to me. Change one line in some improbable and uncertain
future vs instantly find several bugs.




John Carter                             Phone : (64)(3) 358 6639
Tait Electronics                        Fax   : (64)(3) 359 4632
PO Box 1645 Christchurch                Email : john.carter / tait.co.nz
New Zealand