From: avi@...4.com (Avi Bryant) Date: 2002-02-14T05:56:05+09:00 Subject: Re: qualms about respond_to? idiom Received: from lethe.cardhealth.com ([143.98.10.68]) by cardhealth.com (Lotus SMTP MTA v4.6.7 (934.1 12-30-1999)) with SMTP id 88256B5F.0072E995; Wed, 13 Feb 2002 12:55:09 -0800 Received: (from uucp@localhost) by lethe.cardhealth.com (8.9.3 (PHNE_18546)/8.9.3) id MAA18250 for ; Wed, 13 Feb 2002 12:55:41 -0800 (PST) Received: from helium.ruby-lang.org(210.251.121.214) by lethe.cardhealth.com via csmap (V6.0) id srcAAAa18246; Wed, 13 Feb 02 12:55:41 -0800 Received: from helium.ruby-lang.org (localhost [127.0.0.1]) by helium.ruby-lang.org (Postfix) with ESMTP id C421436F; Thu, 14 Feb 2002 05:54:56 +0900 (JST) Received: from zip.local.thomases.com (pragdave210.august.net [216.87.136.210]) by helium.ruby-lang.org (Postfix) with ESMTP id 4EC7A317 for ; Thu, 14 Feb 2002 05:54:55 +0900 (JST) Received: from zip (dave@localhost [127.0.0.1]) by zip.local.thomases.com (8.11.2/8.11.2) with ESMTP id g1DKswW07107 for ; Wed, 13 Feb 2002 14:54:58 -0600 Date: Thu, 14 Feb 2002 05:54:56 +0900 Posted: 13 Feb 2002 12:50:47 -0800 From: avi@beta4.com (Avi Bryant) Reply-To: ruby-talk@ruby-lang.org Subject: Re: qualms about respond_to? idiom To: ruby-talk@ruby-lang.org (ruby-talk ML), undisclosed-recipients: ; Message-Id: <6e869a6b.0202131250.52bc6da0@posting.google.com> References: X-ML-Name: ruby-talk X-Mail-Count: 33792 X-MLServer: fml [fml 3.0pl#17]; post only (anyone can post) X-ML-Info: If you have a question, please contact ruby-talk-admin@ruby-lang.org; Path: news5.aus1.giganews.com!firehose2!nntp3.aus1.giganews.com!border1.nntp.aus1.giganews.com!nntp.giganews.com!hub1.nntpserver.com!headwall.stanford.edu!newsfeed.stanford.edu!postnews1.google.com!not-for-mail Newsgroups: comp.lang.ruby Organization: http://groups.google.com/ NNTP-Posting-Host: 24.68.1.127 X-Trace: posting.google.com 1013633448 21037 127.0.0.1 (13 Feb 2002 20:50:48 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 13 Feb 2002 20:50:48 GMT X-rubymirror: yes Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit Precedence: bulk Lines: 23 > 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}