From: Rob Biedenharn Date: 2009-02-26T05:39:42+09:00 Subject: Re: Using "rescue" inline On Feb 25, 2009, at 2:54 PM, Iñaki Baz Castillo wrote: > El Miércoles, 25 de Febrero de 2009, Rob Biedenharn escribió: > >> 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.) > > Really good point. But I've done some benchmarks comparing both > approache and > using "rescue" is ~ 0.30e-5 faster than your approach (even if yours > seems > more ellegant to me). It depends on how often myvar.downcase raises an exception. If that's really an exceptional occurrence, then just rescuing the occasional failure is the Ruby way. >> 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? > > Not exactly, but myvar could be a Fixnum so othervar would also be a > Fixnum. > But if myvar is a String then I want othervar to be the same String > but > downcase. > > > Thanks a lot. > -- > Iñaki Baz Castillo de nada -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com