On Mon, 11 Oct 2010 16:33:22 -0500, "David A. Black" <dblack / rubypal.com> wrote in <alpine.LFD.2.00.1010111728350.31411 / rubyclinic.com>: >On Tue, 12 Oct 2010, 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? > >It actually will work if var has not been initialized before. But that's >more or less an artifact of how the parser treats local variable >assignment expressions, and is pretty fragile: Exactly. Plus, it's programming by side effect, which gives me the willies. I wondered if someone would bring it up, though. :) [snip proof] -- Charles Calvert Moderator - alt.computer.consultants.moderated Submission Address: accm / celticwolf.net Contact Address: accm_mod / celticwolf.net