Forum: Ruby Rub 1.9: "inline rescue" doesn't work?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-03-02 01:00
(Received via mailing list)
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.
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-03-02 01:03
(Received via mailing list)
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?

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]
0f1f17ba297242e9d3c86d4cc0a6ea85?d=identicon&s=25 Iñaki Baz Castillo (Guest)
on 2009-03-02 01:25
(Received via mailing list)
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.
B44ab09b79ee4a0cc4b4ca69e10eeb3a?d=identicon&s=25 Brian Mitchell (Guest)
on 2009-03-02 01:31
(Received via mailing list)
On Sun, Mar 1, 2009 at 19:01, Iñaki Baz Castillo <ibc@aliax.net> wrote:
> rescue
>
>  $ 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.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-03-02 03:02
(Received via mailing list)
On Sun, Mar 1, 2009 at 6:58 PM, Iñaki Baz Castillo <ibc@aliax.net> wrote:
> 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
F889bf17449ffbf62345d2b2d316a937?d=identicon&s=25 Michal Suchanek (Guest)
on 2009-03-02 09:21
(Received via mailing list)
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.

Thanks

Michal
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2009-03-02 11:40
(Received via mailing list)
On Mon, Mar 2, 2009 at 9:23 AM, Iñaki Baz Castillo <ibc@aliax.net>
wrote:
> 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.

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

^ manveru
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-03-02 14:54
(Received via mailing list)
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.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-03-02 15:38
(Received via mailing list)
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
851acbab08553d1f7aa3eecad17f6aa9?d=identicon&s=25 Ken Bloom (Guest)
on 2009-03-02 16:40
(Received via mailing list)
On Mon, 02 Mar 2009 09:36:10 -0500, Gregory Brown wrote:

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


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
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-03-02 16:55
(Received via mailing list)
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.

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
  def try(sym, *args)
     return if nil?
     send(sym, *args) if respond_to?(sym)
  end
end

then, you get:

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

But there are obviously some other limitations here...

-greg
This topic is locked and can not be replied to.