Trans
4/2/2006 1:54:00 AM
dblack@wobblini.net wrote:
> Hi --
>
> On Sun, 2 Apr 2006, Trans wrote:
>
> > <blockquote>
> > It's not actually that practical, and such things end up making your
> > code very much like C++ and Java.
> >
> > Ruby is smarter than that. Ruby can do more than that.
> >
> > Think in terms of what your object's required capabilities are instead
> > of pretending that a class indicator is sufficient for that.
> > </blockquote>
> >
> > While I understand you pointr Austin --obviously where talking Duck
> > Typing here. But I think it is interesting to condier that this is some
> > respect antithetical to OOP in general --I mean the reciever _is_ a
> > specific type. And that reacieve detemine the functionality of the
> > method call. It is sort of as if you were progamming in a more
> > traditional functional language and _had_ to specifiy the type of the
> > first argument, but never the remaining.
> >
> > foofunc( FooClass foo, clever, smart, stupid )
> >
> > instead of
> >
> > foo.foofunc( clever, smart, stupid )
>
> It's actually more like this:
>
> foofunc(object_that_responds_to_foofunc, etc.)
>
> The fact that an object handles a message does not imply its class.
> (I'm transliterating 'type' to 'class' as that seems to be what the
> thread is actually about [as it usually is :-].)
But the class of that object dictates the functionality of that
message. That's my point --it's class based. A double dispath makes
this very clear:
class String
def from( obj )
# the parameter is type String no matter what
obj.to( self )
end
end
We can even do some fancy dispatching to achieve type parameters:
class String
def cool( *args )
args.shift.cool_string( self, *args )
end
end
class Hash
def cool_string( str, *args )
args.shift.cool_string_hash( str, self )
end
end
class Boolean
def cool_string_hash( str, hsh )
p str.class, hsh.class, self.class
end
end
def cool( clever, smart, stupid )
clever.cool( smart, stupid )
end
cool( "a string", { :a=>'hash' }, Boolean.new )
produces
String
Hash
Boolean
but
cool( 1, 2, 3 ) # => NoMethodError
T.