From: Eric Mahurin Date: 2005-08-04T06:27:58+09:00 Subject: Re: [RCR] array or with non-array --- "David A. Black" wrote: > On Thu, 4 Aug 2005, Eric Mahurin wrote: > > > --- "David A. Black" wrote: > > > >> class != type. Type is at the heart of duck-typing; class > >> isn't :-) > > > > > > Huh? Where do you get that? Duck-typing is about not > caring > > about what the type/class the object is. Duck-typing in > the > > Class is irrelevant, but you'd be well-advised to care about > what the > type is, or you'll find yourself with a lot of NoMethodErrors > :-) > > > purest sense is calling the methods of the arguments > without > > regard to what the class of the object is. > > An object's type is the sum of all of its capabilities. Duck > typing > involves focusing on an object's capabilities -- its type -- > and not > worrying about its class. You also don't have to "worry" > about its > type (and I agree with those who say that duck typing does > not mean > sticking respond_to? everywhere). But you are dealing with > type -- > dealing with it, in the sense of making assumptions about it, > writing > code governed by those assumptions, etc. -- and not dealing > with > class. > > I like Jim Weirich's characterization of Ruby as "a > duck-typed > language", because it captures the fact that, in a sense, one > is > always doing duck typing in Ruby. That's also why I always > feel > uneasy with meta-concepts like the "duck type" of an object. > What > people sometimes call the "duck type" of an object, I call > simply the > type of the object. Duck typing, in that respect, is a way > of > describing an approach to programming that arises very > organically > from Ruby. Whatever your definition of type is, a method should not care (i.e. check) about the types of its arguments. It should just assume that the arguments have the capabilities that it wants. That is is core of duck-typing, is it not? I also think your definition of type (or duck-type) is too restrictive. Instead of "An object's type is the sum of all of its capabilities", it should be "An argument's type is the sum of its capabilities that the method uses or documents that it may use". Every method argument has its own duck-type. But, back to the original thing that spawned this discussion - the String#[] method. I claim that this violates the spirit of duck-typing all over the place because of the class (or type) checking done to accomplish multiple functions. Do you claim it is duck-typed? If so, show me how you could get the multiple functions that it has and still be duck-typed. My point is that when coding ruby, we are encouraged to use duck-typing, but the ruby library itself does not. Seems kind of hypocritical. ____________________________________________________ Start your day with Yahoo! - make it your home page http://www.yahoo.com/r/hs