# Using "rescue" inline

**URL:** <https://rubytalk.org/t/using-rescue-inline/51973>\
**Category:** ruby-talk\
**Created:** [25 February 2009 17:11 UTC](https://rubytalk.org/t/using-rescue-inline/51973 "2009-02-25T17:11:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [25 February 2009 17:11 UTC](https://rubytalk.org/t/using-rescue-inline/51973/1 "2009-02-25T17:11:50Z")

</div>

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  
> \<[ibc@aliax.net](mailto:ibc@aliax.net)\>

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [25 February 2009 18:00 UTC](https://rubytalk.org/t/using-rescue-inline/51973/2 "2009-02-25T18:00:20Z")

</div>

Iñaki Baz Castillo \<ibc@aliax.net\> writes:

> 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.

Naturally, "correctness" depends on what you're trying to  
accomplish. That is one valid use of rescue as a statement modifier  
which will assign the value of myvar.downcase to othervar unless the  
downcase method raises an exception, in which case it will assign  
myvar instead. You can also use more than one rescue as in:

x = foo(y) rescue bar(y) rescue baz(y) rescue nil

> **···**
>
> --  
> Brian Adkins  
> [http://lojic.com/](http://lojic.com/)

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [25 February 2009 18:05 UTC](https://rubytalk.org/t/using-rescue-inline/51973/3 "2009-02-25T18:05:36Z")

</div>

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.

Yes. What you have written is shorthand form of rescue, equivalent to:

myvar = 1  
othervar = begin  
&nbsp;&nbsp;myvar.downcase  
rescue StandardError  
&nbsp;&nbsp;myvar  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [25 February 2009 18:06 UTC](https://rubytalk.org/t/using-rescue-inline/51973/4 "2009-02-25T18:06:30Z")

</div>

Hmm, "correct"? Well, it is certainly legal syntax and is equivalent to:

othervar = begin  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myvar.downcase  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rescue  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myvar  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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,  
&nbsp;&nbsp;&nbsp;myvar = nil  
&nbsp;&nbsp;&nbsp;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:  
&nbsp;&nbsp;&nbsp;othervar = myvar.to\_s.downcase

In any case, you'd have to decide.

-Rob

Rob Biedenharn [http://agileconsultingllc.com](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> 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  
> > \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Herman\_Martinelli](https://avatars.discourse-cdn.com/v4/letter/h/edb3f5/32.png) [@Herman\_Martinelli](https://rubytalk.org/u/Herman_Martinelli)\
**Post date:** [25 February 2009 18:49 UTC](https://rubytalk.org/t/using-rescue-inline/51973/5 "2009-02-25T18:49:26Z")

</div>

Brian Adkins wrote:

> Naturally, "correctness" depends on what you're trying to  
> accomplish.

LOL  
That's really a good one (in addition to the valuable Ruby answer).

H.

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [25 February 2009 19:54 UTC](https://rubytalk.org/t/using-rescue-inline/51973/6 "2009-02-25T19:54:21Z")

</div>

> 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 also can hide problems that you won't know that you have. For  
> example,  
> &nbsp;&nbsp;&nbsp;myvar = nil  
> &nbsp;&nbsp;&nbsp;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.

> **···**
>
> El Miércoles, 25 de Febrero de 2009, Rob Biedenharn escribió:
> 
> --  
> Iñaki Baz Castillo

---

<div class="post-metadata">

**Author:** ![Rob\_Biedenharn1](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rob\_Biedenharn1](https://rubytalk.org/u/Rob_Biedenharn1)\
**Post date:** [25 February 2009 20:39 UTC](https://rubytalk.org/t/using-rescue-inline/51973/7 "2009-02-25T20:39:42Z")

</div>

> > 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,  
> > &nbsp;&nbsp;myvar = nil  
> > &nbsp;&nbsp;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](http://agileconsultingllc.com)  
Rob@AgileConsultingLLC.com

> **···**
>
> On Feb 25, 2009, at 2:54 PM, Iñaki Baz Castillo wrote:
> 
> > El Miércoles, 25 de Febrero de 2009, Rob Biedenharn escribió:

---

<div class="post-metadata">

**Author:** ![lasitha](https://avatars.discourse-cdn.com/v4/letter/l/4af34b/32.png) [@lasitha](https://rubytalk.org/u/lasitha)\
**Post date:** [26 February 2009 11:26 UTC](https://rubytalk.org/t/using-rescue-inline/51973/8 "2009-02-26T11:26:26Z")

</div>

This may be stating the obvious but code that branches based on type  
is smelly. That is not to say it's always wrong, just always at least  
a little smelly :). Particularly if you find multiple places in the  
code having to deal with Fixnum and String conditionally.

The inline rescue slightly obscures the conditional but the smell  
remains. It may be worthwhile reconsidering whether you really want  
myvar to hold disparate types.

I would also reiterate Rob's concern that this rescue could easily  
hide some unrelated bug (mvay being nil being the most obvious).

These two concerns would be enough for me to reconsider, but as usual,  
it all depends on context.

Cheers,  
lasitha

> **···**
>
> On Thu, Feb 26, 2009 at 1:24 AM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> 
> > > 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.

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [26 February 2009 14:29 UTC](https://rubytalk.org/t/using-rescue-inline/51973/9 "2009-02-26T14:29:52Z")

</div>

Yes, you are right, I'll consider it. Thanks a lot.

> **···**
>
> 2009/2/26 lasitha \<lasitha.ranatunga@gmail.com\>:
> 
> > On Thu, Feb 26, 2009 at 1:24 AM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> > 
> > > > 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.
> > 
> > This may be stating the obvious but code that branches based on type  
> > is smelly. That is not to say it's always wrong, just always at least  
> > a little smelly :). Particularly if you find multiple places in the  
> > code having to deal with Fixnum and String conditionally.
> > 
> > The inline rescue slightly obscures the conditional but the smell  
> > remains. It may be worthwhile reconsidering whether you really want  
> > myvar to hold disparate types.
> > 
> > I would also reiterate Rob's concern that this rescue could easily  
> > hide some unrelated bug (mvay being nil being the most obvious).
> > 
> > These two concerns would be enough for me to reconsider, but as usual,  
> > it all depends on context.
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>
