From: Pit Capitain Date: 2002-02-14T02:14:58+09:00 Subject: Re: qualms about respond_to? idiom On 14 Feb 2002, at 0:57, David Alan Black wrote on usage of respond_to? > 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. > This is a case where I wish I knew a way to avoid the duplication, > which feels really clunky to me. I don't know what you'd like to improve. In your code the further processing depends on the result of the method call. Would you like something like this better: class Object def send_if_respond_to( else_result, method, *args, &block ) if respond_to? method send( method, *args, &block ) else else_result end end end p "abc".send_if_respond_to( 5, :size ) # => 3 p :abc.send_if_respond_to( 5, :size ) # => 5 or in your case if e.send_if_respond_to( false, :tagStart? ) context.push(e.name) end I don't know if something like this feels better. If we had an Objective-C or Smalltalk like syntax it could be written as e.send_if_respond_to: :tagStart? else: false Regards, Pit