On 10/11/2010 01:05 PM, Charles Calvert wrote: > I'm using Ruby 1.8.7 patchlevel 249 > > Is there a more idiomatic way to do the following? > > var = hash[key].nil? ? nil : hash[key].downcase > > Note that if hash[key] is nil, I want nil assigned to var, so this > won't work: > > var = hash[key].downcase unless hash[key].nil? > > Obviously I could do this, but I'm trying to keep it on one line: > > var = hash[key] > var = var.downcase unless var.nil? cheat a little? var = hash[key]; var &&= var.downcase another idea: class Object def and_then yield self end end class NilClass def and_then self end end var = hash[key].and_then {|val| val.downcase}