From: Charles Calvert Date: 2010-10-12T12:35:10+09:00 Subject: Re: more idiomatic way to avoid errors when calling method on variable that may be nil? On Mon, 11 Oct 2010 19:26:46 -0700 (PDT), KOTP wrote in <517830db-a8e6-48ef-b8bb-dd9707cb35e7@c10g2000yqh.googlegroups.com>: >On Oct 11, 3:02�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 > >This is identical to saying this: >var = hash[key] && hash[key].to_s.downcase In my use case, true, because the type of the value will either be NilClass or String. As Josh Cheek pointed out, though, if hash[key] returns false, var would be false, rather than "false". Thanks for the suggestion. -- Charles Calvert Moderator - alt.computer.consultants.moderated Submission Address: accm@celticwolf.net Contact Address: accm_mod@celticwolf.net