Forum: Ruby Weird rescue

Posted by "Kai König" <kai@kairichardkoenig.de> (Guest)
on 2012-12-04 19:14
(Received via mailing list)
Can somebody tell why this happens ?


x = StandardError.new(:hello)
y = StandardError.new(:hello)
puts x == y # => true
puts x === y # => true

begin
  raise x
rescue x
  puts "ok" # gets printed
end

begin
  raise x
rescue y
  puts "ok" # doesn't get printed
end
Posted by botp (Guest)
on 2012-12-04 20:47
(Received via mailing list)
On Wed, Dec 5, 2012 at 2:13 AM, Kai König <kai@kairichardkoenig.de> 
wrote:

> x = StandardError.new(:hello)
>
> y = StandardError.new(:hello)
>


x and y are different objects.  try compare with #equal?

you may need

y=x

to force..

best regards -botp
Posted by unknown (Guest)
on 2012-12-04 20:52
(Received via mailing list)
Am 04.12.2012 19:13, schrieb Kai K
Posted by Eric Christopherson (echristopherson)
on 2012-12-05 00:08
(Received via mailing list)
On Tue, Dec 4, 2012 at 12:13 PM, Kai König <kai@kairichardkoenig.de> 
wrote:
> rescue x
>   puts "ok" # gets printed
> end
>
> begin
>   raise x
> rescue y
>   puts "ok" # doesn't get printed
> end
>

That is strange. According to the Pickaxe[1] (the online one at least;
I don't have my 1.9 copy handy) the exception matching is supposed to
be done with kind_of?; and the source code[2] seems to bear that out.
Clearly StandardError.new(:hello).kind_of?(StandardError.new(:hello))
is not true; it raises a TypeError because the argument isn't a class
or module.

[1] 
http://www.ruby-doc.org/docs/ProgrammingRuby/html/...
[2] http://rxr.whitequark.org/mri/source/eval.c#714
Posted by Eric Christopherson (echristopherson)
on 2012-12-05 01:59
(Received via mailing list)
On Tue, Dec 4, 2012 at 5:01 PM, Eric Christopherson
<echristopherson@gmail.com> wrote:
>>   raise x
>
> That is strange. According to the Pickaxe[1] (the online one at least;
> I don't have my 1.9 copy handy) the exception matching is supposed to
> be done with kind_of?; and the source code[2] seems to bear that out.
> Clearly StandardError.new(:hello).kind_of?(StandardError.new(:hello))
> is not true; it raises a TypeError because the argument isn't a class
> or module.
>
> [1] http://www.ruby-doc.org/docs/ProgrammingRuby/html/...
> [2] http://rxr.whitequark.org/mri/source/eval.c#714
>

OK, so apparently this has been answered on StackOverflow:
http://stackoverflow.com/questions/13709100/what-i...
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.