From: avi@...4.com (Avi Bryant) Date: 2002-02-14T05:54:56+09:00 Subject: Re: qualms about respond_to? idiom > 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. Ok, how about: class Object def try_sending(msg, *args) if respond_to? msg yield send(msg, *args) end end end e.try_sending(:tagStart?) {|s| context.push(e.name) if s}