On Dec 5, 5:48 pm, John Carter <john.car... / tait.co.nz> wrote:
> Is there another library like this? I would love it if it were just
> standard methods on Object.
>
> I find this very useful and 'require' it into all code I write. Just
> helps me mop up those problems that static type checking would have
> found for me...
>
> Heres a typical usage, create an object and initialize it with
> stuff. Only much later when you invoke the methods on the object do
> you find you initialized it with the wrong stuff.
>
> Got the parameter order wrong or something. But then the bug isn't on
> the backtrace. So put when you initialize your instance variables you
> can type check'em like so...
>
> eg.
> class MyObject
>    def initialize( _name, _drawable, _theme)
>      @name     = _name.static_type_check String
>      @drawable = _drawable.polymorphic_type_check Drawable
>      @theme    = _theme.quacks_like :border_color, :fill_colour
>    end
>
> ..
>
> end
>
> # Abstract base class for all the type check exceptions
> class TypeCheckException  < Exception
> end
>
> # This exception is thrown in event of a method being invoked with an
> # object of the wrong duck type.
> class DuckTypingException < TypeCheckException
> end
>
> # This exception is thrown in event of a method being invoked with a
> # parameter of of the wrong static type.
> class StaticTypeException < TypeCheckException
> end
>
> # This exception is thrown in event of a method being invoked with a
> # parameter of of the wrong polymorphic type.
> class PolymorphicTypeException < TypeCheckException
> end
>
> class Object
>     # Raise a DuckTypingException unless the object responds to all these symbols.
>     def quacks_like( *symbols)
>        symbols.each do |symbol|
>           raise DuckTypingException, "Duck typing error, expected this object to respond to :#{symbol}, but found class #{self.class}\n\t\t#{symbol.inspect}" unless
>              respond_to? symbol
>        end
>        self
>     end
>
>     def static_type_check_boolean
>        raise StaticTypeException, "Static type check error, expected object to be a boolean, found '#{self.class}'
> \t\t#{self.inspect}" unless
>           (self.class == TrueClass) ||
>           (self.class == FalseClass) ||
>           (self.class == NilClass)
>        self
>     end
>
>     def static_type_check( klass)
>        raise StaticTypeException, "Static type check error, expected object to be exactly class '#{klass}', found '#{self.class}'\n\t\t#{self.inspect}" unless
>           self.class == klass
>        self
>     end
>
>     def static_type_check_each( klass)
>        each do |object|
>           object.static_type_check klass
>        end
>        self
>     end
>
>     def polymorphic_type_check_each( klass)
>        each do |object|
>           object.polymorphic_type_check klass
>        end
>        self
>     end
>
>     def polymorphic_type_check( klass)
>        raise PolymorphicTypeException, "Polymorphic type check error, expected object to be a kind of '#{klass}', found '#{self.class}'\n\t\t#{self.inspect}" unless
>           self.kind_of? klass
>        self
>     end
>
> end
>
> John Carter                             Phone : (64)(3) 358 6639
> Tait Electronics                        Fax   : (64)(3) 359 4632
> PO Box 1645 Christchurch                Email : john.car... / tait.co.nz
> New Zealand

Ola Bini released something similar last year [1]. It didn't go over
very well (though I liked it and have used it a couple times). YMMV.

Regards,
Jordan

[1] http://rubyforge.org/projects/ducktator/