# Rub 1.9: "inline rescue" doesn't work?

**URL:** <https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065>\
**Category:** ruby-talk\
**Created:** [1 March 2009 23:58 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065 "2009-03-01T23:58:46Z")\
**Posts on this page:** 11\
**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:** [1 March 2009 23:58 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/1 "2009-03-01T23:58:46Z")

</div>

Hi, is there any explanation for the folowing big difference between the same  
code in 1.8 and 1.9?:

1.8)

aaa = 123.capitalize rescue 444  
=\> 444

1.9)

aaa = 123.capitalize rescue 444  
NoMethodError: undefined method `capitalize' for 123:Fixnum

It seems that Ruby 1.9 doesn't react on "inline rescue" as 1.8. Do I miss  
something?

Thanks a lot.

> **···**
>
> --  
> Iñaki Baz Castillo

---

<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:** [2 March 2009 00:01 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/2 "2009-03-02T00:01:25Z")

</div>

Well, it must be something even worse since block rescue neither works:

> **···**
>
> El Lunes, 2 de Marzo de 2009, Iñaki Baz Castillo escribió:
> 
> > It seems that Ruby 1.9 doesn't react on "inline rescue" as 1.8. Do I miss  
> > something?
> 
> -----------------  
> begin  
> &nbsp;&nbsp;aaa = 123.capitalize  
> rescue  
> &nbsp;&nbsp;aaa = 444  
> end  
> ------------------  
> =\> NoMethodError: undefined method `capitalize' for 123:Fixnum
> 
> Perhaps it's due to the not updated version of Ruby1.9 I'm using?:
> 
> &nbsp;&nbsp;$ irb1.9 -v  
> &nbsp;&nbsp;irb 0.9.5(05/04/13)
> 
> &nbsp;&nbsp;$ ruby1.9 -v  
> &nbsp;&nbsp;ruby 1.9.0 (2007-08-30 patchlevel 0) [i486-linux]
> 
> --  
> Iñaki Baz Castillo

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [2 March 2009 02:01 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/3 "2009-03-02T02:01:02Z")

</div>

> Hi, is there any explanation for the folowing big difference between the same  
> code in 1.8 and 1.9?:
> 
> 1.8)
> 
> aaa = 123.capitalize rescue 444  
> =\> 444
> 
> 1.9)
> 
> aaa = 123.capitalize rescue 444  
> NoMethodError: undefined method `capitalize' for 123:Fixnum

> > RUBY\_VERSION

=\> "1.9.1"

> > 123.captialize rescue 444

=\> 444

But please, don't use this technique. It creates huge debugging nightmares.

Instead, just do:

aaa = obj.respond\_to?(:capitalize) ? obj.capitalize : 444

> **···**
>
> On Sun, Mar 1, 2009 at 6:58 PM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:

---

<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:** [2 March 2009 00:23 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/4 "2009-03-02T00:23:18Z")

</div>

Yes, it seems to be an old bug in my 1.9 version. I've upgraded to:

&nbsp;&nbsp;$ ruby1.9 -v  
&nbsp;&nbsp;ruby 1.9.0 (2007-12-25 revision 14709) [i486-linux]

and the "rescue" issue has gone.

> **···**
>
> El Lunes, 2 de Marzo de 2009, Iñaki Baz Castillo escribió:
> 
> > Perhaps it's due to the not updated version of Ruby1.9 I'm using?:
> > 
> > &nbsp;&nbsp;$ irb1.9 -v  
> > &nbsp;&nbsp;irb 0.9.5(05/04/13)
> > 
> > &nbsp;&nbsp;$ ruby1.9 -v  
> > &nbsp;&nbsp;ruby 1.9.0 (2007-08-30 patchlevel 0) [i486-linux]
> 
> --  
> Iñaki Baz Castillo

---

<div class="post-metadata">

**Author:** ![Brian\_Mitchell](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Brian\_Mitchell](https://rubytalk.org/u/Brian_Mitchell)\
**Post date:** [2 March 2009 00:29 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/5 "2009-03-02T00:29:37Z")

</div>

> > It seems that Ruby 1.9 doesn't react on "inline rescue" as 1.8. Do I miss  
> > something?
> 
> Well, it must be something even worse since block rescue neither works:
> 
> -----------------  
> begin  
> aaa = 123.capitalize  
> rescue  
> aaa = 444  
> end  
> ------------------  
> =\> NoMethodError: undefined method `capitalize' for 123:Fixnum
> 
> Perhaps it's due to the not updated version of Ruby1.9 I'm using?:
> 
> $ irb1.9 -v  
> irb 0.9.5(05/04/13)
> 
> $ ruby1.9 -v  
> ruby 1.9.0 (2007-08-30 patchlevel 0) [i486-linux]

miro:~ brian$ irb19

> > aaa = 123.capitalize rescue 444

=\> 444  
miro:~ brian$ ruby19 -v  
ruby 1.9.2dev (2009-03-02 trunk 22700) [i386-darwin9.6.0]

Brian.

> **···**
>
> On Sun, Mar 1, 2009 at 19:01, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> 
> > El Lunes, 2 de Marzo de 2009, Iñaki Baz Castillo escribió:

---

<div class="post-metadata">

**Author:** ![Michal\_hramrach\_Such](https://avatars.discourse-cdn.com/v4/letter/m/3ab097/32.png) [@Michal\_hramrach\_Such](https://rubytalk.org/u/Michal_hramrach_Such)\
**Post date:** [2 March 2009 08:18 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/6 "2009-03-02T08:18:35Z")

</div>

I'd see it as a Ruby's failure that the proper check is against DRY.

Thanks

Michal

> **···**
>
> 2009/3/2 Gregory Brown \<gregory.t.brown@gmail.com\>:
> 
> > On Sun, Mar 1, 2009 at 6:58 PM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> > 
> > > Hi, is there any explanation for the folowing big difference between the same  
> > > code in 1.8 and 1.9?:
> > > 
> > > 1.8)
> > > 
> > > aaa = 123.capitalize rescue 444  
> > > =\> 444
> > > 
> > > 1.9)
> > > 
> > > aaa = 123.capitalize rescue 444  
> > > NoMethodError: undefined method `capitalize' for 123:Fixnum
> > 
> > > > RUBY\_VERSION
> > 
> > =\> "1.9.1"
> > 
> > > > 123.captialize rescue 444
> > 
> > =\> 444
> > 
> > But please, don't use this technique. It creates huge debugging nightmares.
> > 
> > Instead, just do:
> > 
> > aaa = obj.respond\_to?(:capitalize) ? obj.capitalize : 444

---

<div class="post-metadata">

**Author:** ![Michael\_Fellinger1](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@Michael\_Fellinger1](https://rubytalk.org/u/Michael_Fellinger1)\
**Post date:** [2 March 2009 10:38 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/7 "2009-03-02T10:38:15Z")

</div>

That's still ancient. The latest revision is 22705 and the last stable  
release was 1.9.1.

^ manveru

> **···**
>
> On Mon, Mar 2, 2009 at 9:23 AM, Iñaki Baz Castillo \<ibc@aliax.net\> wrote:
> 
> > El Lunes, 2 de Marzo de 2009, Iñaki Baz Castillo escribió:
> > 
> > > Perhaps it's due to the not updated version of Ruby1.9 I'm using?:
> > > 
> > > $ irb1.9 -v  
> > > irb 0.9.5(05/04/13)
> > > 
> > > $ ruby1.9 -v  
> > > ruby 1.9.0 (2007-08-30 patchlevel 0) [i486-linux]
> > 
> > Yes, it seems to be an old bug in my 1.9 version. I've upgraded to:
> > 
> > $ ruby1.9 -v  
> > ruby 1.9.0 (2007-12-25 revision 14709) [i486-linux]
> > 
> > and the "rescue" issue has gone.

---

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [2 March 2009 13:48 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/8 "2009-03-02T13:48:49Z")

</div>

Michal Suchanek wrote:

> > aaa = obj.respond\_to?(:capitalize) ? obj.capitalize : 444

> I'd see it as a Ruby's failure that the proper check is against DRY.

DRY is most egregious when the duplicated components are far apart from each other. (And hence harder to spot!) Duplicating things right next to each other is Mostly Harmless, and it's always the next best thing if you can't think of the final refactor.

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;Phlip  
> &nbsp;&nbsp;&nbsp;[http://www.zeroplayer.com/](http://www.zeroplayer.com/)

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [2 March 2009 14:36 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/9 "2009-03-02T14:36:10Z")

</div>

Agreed, but the costs of the elegance of using rescue as a conditional  
modifier stack up fast in any moderately complex system.

-greg

> **···**
>
> On Mon, Mar 2, 2009 at 3:18 AM, Michal Suchanek \<hramrach@centrum.cz\> wrote:
> 
> > 2009/3/2 Gregory Brown \<gregory.t.brown@gmail.com\>:
> 
> > > But please, don't use this technique. It creates huge debugging nightmares.
> > > 
> > > Instead, just do:
> > > 
> > > aaa = obj.respond\_to?(:capitalize) ? obj.capitalize : 444
> > 
> > I'd see it as a Ruby's failure that the proper check is against DRY.
> 
> --  
> Technical Blaag at: [http://blog.majesticseacreature.com](http://blog.majesticseacreature.com)  
> Non-tech stuff at: [http://metametta.blogspot.com](http://metametta.blogspot.com)  
> "Ruby Best Practices" Book now in O'Reilly Roughcuts:  
> [http://rubybestpractices.com](http://rubybestpractices.com)

---

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [2 March 2009 15:38 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/10 "2009-03-02T15:38:55Z")

</div>

The problem with the rescue modifier is that it lumps all types of errors  
into one relatively blunt tool. A rescue(Type) modifier would really  
help, and a ?. operator (like Groovy's) would resolve one of the most  
common cases for a rescue modifier.

--Ken

> **···**
>
> On Mon, 02 Mar 2009 09:36:10 -0500, Gregory Brown wrote:
> 
> > On Mon, Mar 2, 2009 at 3:18 AM, Michal Suchanek \<hramrach@centrum.cz\> \> wrote:
> > 
> > > 2009/3/2 Gregory Brown \<gregory.t.brown@gmail.com\>:
> > 
> > > > But please, don't use this technique. It creates huge debugging  
> > > > nightmares.
> > > > 
> > > > Instead, just do:
> > > > 
> > > > aaa = obj.respond\_to?(:capitalize) ? obj.capitalize : 444
> > > 
> > > I'd see it as a Ruby's failure that the proper check is against DRY.
> > 
> > Agreed, but the costs of the elegance of using rescue as a conditional  
> > modifier stack up fast in any moderately complex system.
> > 
> > -greg
> 
> --  
> Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![Greg\_Brown1](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@Greg\_Brown1](https://rubytalk.org/u/Greg_Brown1)\
**Post date:** [2 March 2009 15:53 UTC](https://rubytalk.org/t/rub-1-9-inline-rescue-doesnt-work/52065/11 "2009-03-02T15:53:42Z")

</div>

Right, if you could do:

obj.capitalize rescue(NoMethodError) 42

it'd remove the debugging issue. However, this does force the  
interpreter to do a whole lot of extra work raising and rescuing an  
error unnecessarily.

Groovy's ?. looks about right in terms of what we really want most of  
the time rescue is used as a conditional modifier.  
In the past, I've implemented this as:

class Object  
&nbsp;&nbsp;def try(sym, \*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return if nil?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;send(sym, \*args) if respond\_to?(sym)  
&nbsp;&nbsp;end  
end

then, you get:

a = obj.try(:capitalize) || 42

But there are obviously some other limitations here...

-greg

> **···**
>
> On Mon, Mar 2, 2009 at 10:38 AM, Ken Bloom \<kbloom@gmail.com\> wrote:
> 
> > The problem with the rescue modifier is that it lumps all types of errors  
> > into one relatively blunt tool. A rescue(Type) modifier would really  
> > help, and a ?. operator (like Groovy's) would resolve one of the most  
> > common cases for a rescue modifier.
> 
> --  
> Technical Blaag at: [http://blog.majesticseacreature.com](http://blog.majesticseacreature.com)  
> Non-tech stuff at: [http://metametta.blogspot.com](http://metametta.blogspot.com)  
> "Ruby Best Practices" Book now in O'Reilly Roughcuts:  
> [http://rubybestpractices.com](http://rubybestpractices.com)
