From: Dave Thomas Date: 2002-02-14T02:23:04+09:00 Subject: Re: qualms about respond_to? idiom Paul Brannan writes: > On Thu, Feb 14, 2002 at 01:11:39AM +0900, Dave Thomas wrote: > > start tags would have > > > > def handle_stack(stack) > > stack.push(name) > > end > > Does this really belong in the stream object itself? Probably not, but I wanted to keep it simple. 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? Dave