From: mathew Date: 2006-02-03T08:46:12+09:00 Subject: ANDCALL / iff? / &? (was Re: ruby-dev summary 28206-28273) > [ruby-dev:28217] ANDCALL operator > > Nobuyoshi Nakada suggested a new operator `&?' (this notation is > temporary) > which evaluates left-hand-side expression, and if it is true then call > right-hand-side method. For example: > > if a[1] and a[1].strip.empty? > || > if a[1] &? strip.empty? > > h["key"] and h["key"].dispatch > || > h["key"] &? dispatch > > The motivation of this operator is to avoid duplication of expression. > > Takaaki Tateishi proposed another idea, Object#nil? with block > (again, this name is temporary). > > a[1].nil? {|str| str.strip.empty? } > h["key"].nil? {|h| h.dispatch } > > This issue is still open. I have to say, I don't like any of these. Personally, I write if a a.foo end because it's simple for anyone to understand, and easy to refactor when you inevitably decide you need to do something else as well as calling the method. If you find it too painful to type the expression twice, I say get a text editor which has dynamic completion, or use a temporary variable if your expression has side effects. > # > # if, and only if > # > > iff?(a.first){ p self } I dislike this less, because at least it's not an operator. I switched from Perl to get away from those. Another option would be to call it 'with', and define that with(nil) doesn't execute the block. Then you could just write with a[1] strip.empty or with a[1] strip empty end which would have the side benefit of allowing you to reduce the length of long lines of chained methods. mathew [ OK, I admit it, I got the idea from BASIC. ]