From: Rob Biedenharn Date: 2009-02-26T03:06:30+09:00 Subject: Re: Using "rescue" inline On Feb 25, 2009, at 12:11 PM, Iņaki Baz Castillo wrote: > Hi, is it correct the following "rescue" usage? > > myvar = 1 # Fixnum > othervar = myvar.downcase rescue myvar > => 1 > > It seems to work, but is it really correct? Thanks. > > -- > Iņaki Baz Castillo > Hmm, "correct"? Well, it is certainly legal syntax and is equivalent to: othervar = begin myvar.downcase rescue myvar end But, it might not be the best way to shave the yak. othervar = myvar.respond_to?(:downcase) ? myvar.downcase : myvar might perform better if the exception to be rescued is expensive to construct only to then be thrown away. (I don't know if there's any special optimization of the expression form of rescue compared to the block form.) It also can hide problems that you won't know that you have. For example, myvar = nil othervar = myvar.downcase rescue myvar Did you want to set othervar to nil also? Perhaps you need othervar as a downcased String? Maybe it would be better as: othervar = myvar.to_s.downcase In any case, you'd have to decide. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com