From: "David A. Black" Date: 2007-10-22T23:56:40+09:00 Subject: Re: Conflicts between using respond_to? and extending Ruby Hi -- On Mon, 22 Oct 2007, Rick DeNatale wrote: > On 10/22/07, David A. Black wrote: > >> On Mon, 22 Oct 2007, Trans wrote: > >>> I recently cam across an interesting conflict between open-uri.rb and >>> Facets rendition of kernel/metaid.rb. Facets defines a general >>> extension Kernel#meta, and open-uri defines OpenURI::Meta#meta. open- >>> uri uses OpenURI::Meta to extend StringIO via singleton. This is all >>> fine, but then open-uri checks to see if a StringIO object has been >>> extended by doing: >>> >>> io.respond_to? :meta >>> >>> And that's where the problem lies, b/c if you've loaded 'facets/kernel/ >>> metaid' you're going to get a false positive here. Now the best remedy >>> is open-uri to be more precise: >>> >>> io.is_a? OpenURI::Meta >>> >>> That fixes the the problem. But it raises an interesting question. >>> I've used #respond_to? myself from time to time thinking it was oh, so >>> duck-type kosher; never realizing that it might clash with general >>> extensions. >>> >>> So where lies the fault in this conflict? Are extensions the bad guy, >>> or is respond_to? really not a good oop concept? Or..? >> >> I think this is just the age-old issue about adding methods to >> existing classes and hitting conflicts. #respond_to? is relatively >> dumb; it can't tell you what the method actually does. I think of it >> as an important tool for "soft" duck typing, in contrast with "hard" >> duck typing where you just send the object the message. > > Okay, I'm claiming stake to coining what I hope becomes as much of a > meme as duck typing: > > http://talklikeaduck.denhaven2.com/articles/2007/10/22/chicken-typing-isnt-duck-typing Be careful what you wish for :-) In any case, I'm glad to have provided the impetus, if not the name. What I've always called the soft/hard duck typing distinction isn't quite the same as what you're calling the chicken/duck distinction, though, in the sense that I rule is_a? and kind_of? completely out of bounds as duck typing of any kind. Duck typing, as I understand it from various writings by, and exchanges with, Dave Thomas, starts where is_a? leaves off. respond_to? starts to enter the duck-typing orbit, in the sense that at least it's focused on the object and its capabilities, rather than its class/module ancestry (and that's the most decisive line to cross). Since it still falls short of simply asking the object to do something, I consider it a "soft" form of duck typing. It's object-driven, but not atomic. David -- Upcoming training by David A. Black/Ruby Power and Light, LLC: * Advancing with Rails, Edison, NJ, November 6-9 * Advancing with Rails, Berlin, Germany, November 19-22 * Intro to Rails, London, UK, December 3-6 (by Skills Matter) See http://www.rubypal.com for details!