From: Devin Mullins Date: 2007-02-15T15:03:59+09:00 Subject: Re: Drying a ternary condition Marcello Barnaba wrote: >>dirs = %w{up down} >>klass.send(dirs.find(dirs.first) {|dir| dir == ARGV[0] }) > > clever use of .first (setting the default) and .find. well, it turns out, not so clever. i got the api wrong - find expects the arg to be a Proc. (also there's a typo in the above.) meh: klass.send(%w{up down}.grep(ARGV[0])[0] || 'up') readability? what's that? > but I think that readability hit a regression.. and first we stood in front of > 3 ups, now we stand in front of 4 dirs! :) Well, it's likely the wrong place for the code, but while we're hunting for [what is probably more textual duplication than duplication of knowledge]: module Enumerable def match_or_first(obj) self[index(obj) || 0] end end klass.send(%w{up down}.match_or_first(ARGV[0]) Don't like the duplicated 'obj'? Class.new(Array) { def index(*a) self[super || 0] end klass.send(new(%w{up down}).index(ARGV[0])) } Now I just to find a way to factor out the duplicated 'new' and '0'... Devin