From: Rich Kilmer Date: 2002-02-14T04:59:02+09:00 Subject: RE: qualms about respond_to? idiom > -----Original Message----- > From: dave@thomases.com [mailto:dave@thomases.com]On Behalf Of Dave > Thomas > Sent: Wednesday, February 13, 2002 12:23 PM > To: ruby-talk ML > Subject: Re: qualms about respond_to? idiom > > > > If this were a big deal, I'd probably arrange to wrap the stream > objects in objects that handled things like the stack manipulation, > delegating the normal stream object calls. > > However, there's also a bit of a smell to the original: > > if e.responds_to? :tagStart > do_somthing if e.tagStart > end > > What are we doing here? All tags have a method that says "yes I'm a > start tag" or "no I'm not a start tag", and we're using that method to > determine if something is a tag. We then _call_ that method to see it > it's a start tag. > > So the next step would be to say "only start tags respond to > :tagStart", and the code becomes > > do_somthing if e.responds_to? :tagStart > > But then that smells: we have a method that we never call, simply so > we can use it as a flag. Hmm, let's have a mixin called StartTag that > anything that is a start tag includes > > module StartTag; end > > Then we have > > do_somthing if e.kind_of? StartTag > > > Does that communicate the intent? > > Regarding Mixins vs. individual methods (respond_to?), I think using a Mixin gives you a larger namespace to manage identification of behaviors (whereas methods are in a single "flat" namespace). module USAF module DEFCON_1 def do_your_worst @missles.launch end end end can be semantically differentiated with kind_of? from: module StateFarmInsurance def do_your_worst @policy.cancel end end whereas (respond_to? :do_your_worst) cannot. -rich