No! :(  Blanket rescue statements are not to be used.  It's a trivial
example, sure, but still.

if hash.has_key?(:key) and hash[:key].is_a?(String)
  # do stuff
end

Or, your rescue example... I'd at least rewrite it as:

var = begin
        hash[key].downcase
      rescue NoMethodError
        nil
      end

If you use Ruby Ketsup (aka, Active Support), you can put this everywhere:

val = hash[key] if hash[key].present?
#or...
val = hash[key] unless hash[key].blank?

So you know why you're rescuing here.  Blanket rescue foolishness can
and will cascade throughout your application.  Again, trivial
example...  But I think it shouldn't really be done in any
circumstance.

Scott

On Mon, Oct 11, 2010 at 1:17 PM, Kirk Haines <wyhaines / gmail.com> wrote:
> On Mon, Oct 11, 2010 at 2:05 PM, Charles Calvert <cbciv / yahoo.com> 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
>
> I see nothing wrong with an explicit approach.    
>
> var = hash[key].downcase rescue nil
>
>
> Kirk Haines
>
>