From: Hugh Sasse Staff Elec Eng Date: 2002-02-14T01:32:37+09:00 Subject: Re: qualms about respond_to? idiom On Thu, 14 Feb 2002, Dave Thomas wrote: > David Alan Black writes: > > > All tags respond to tagStart? and tagEnd? But not all events in my > > stream are tags. So.... > > > > if e.respond_to? :tagStart? > > context.push(e.name) if e.tagStart? > > end > > > > is my way of testing, first, whether it's a tag (but without actually > > checking whether it's a tag), and, second, whether it's a start tag. > > A polymorphic way of doing this is to move responsibility down to the > stream elements. Say each had a method handle_stack. Then your main > code could simply call handle_stack on _all_ tags: > > e.handle_stack(context) > > > start tags would have > > def handle_stack(stack) > stack.push(name) > end So is this an example of "Tell, Don't Ask", or is that more about an object's internal state? It does overcome this problem, but it also means all the objects have to know how to handle a stack. Doesn't that lead to "unnecessary" coupling between objects? > > > end tags might have (I'm guessing - I don't know what your app does > here) [...] > > and all other tags would have a null handle_stack > > def handle_stack(stack) > end What if you get something completely different to put on the stack? I suppose you could put the method in Object, but it seems a bit heavy handed to do that. > > > > Dave > Hugh