From: Rob Biedenharn Date: 2007-02-15T03:53:07+09:00 Subject: Re: Drying a ternary condition On Feb 14, 2007, at 1:33 PM, sur max wrote: >> On 2/14/07, Tim Pease wrote: >>> On 2/14/07, Marcello Barnaba wrote: >>> > How would you write the following statement: >>> > >>> > klass.send((ARGV[0] || :up).to_sym == :up ? :up : :down) >>> >>> ARGV[0] ? klass.send(:up) : klass.send(:down) >>> >>> Blessings, >>> TwP > klass.send(ARGV[0] ? :up : :down) > -- > sur > http://expressica.com Since neither of those does the same as the OP's statement, they can't be right. I'd argue that the statement is pretty DRY already. Your original code says: "If the first argument is "up" or false, call klass.up, otherwise call klass.down" The other responders would call klass.down when ARGV[0] is false/ nil. Also you can't call nil.to_sym (as you probably learned from ARGV[0].to_sym raising a NoMethodError) or "".to_sym (ArgumentError for the empty string). If there were more than two options, something like this might be helpful. >> m=Hash.new(:down).merge!({:up=>:up}) => {:up=>:up} >> [ :up, :down, nil, :fred, "up", "down", "", "fred" ].each do |x| ?> puts "#{x.inspect} => #{m[(x.to_sym rescue :up)].inspect}" >> end :up => :up :down => :down nil => :up :fred => :down "up" => :up "down" => :down "" => :up "fred" => :down => [:up, :down, nil, :fred, "up", "down", "", "fred"] So: klass.send(Hash.new(:down).merge!({:up=>:up})[(ARGV[0].to_sym rescue :up)]) Yuck! Stick with the simple ternary for your statement (although you might want to use the rescue trick instead of just ||). -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com