Weird rescue

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

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

you may need

y=x

to force..

best regards -botp

···

On Wed, Dec 5, 2012 at 2:13 AM, Kai König <kai@kairichardkoenig.de> wrote:

x = StandardError.new(:hello)

y = StandardError.new(:hello)

did you mean:

class MyStandardError < StandardError; end

x = MyStandardError.new(:hello)
y = MyStandardError.new(:hello)

begin
   raise x
rescue MyStandardError
   puts 'ok'
end

# => ok

begin
   raise y
rescue MyStandardError
   puts 'ok'
end

# => ok

···

Am 04.12.2012 19:13, schrieb Kai König:

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

--
<https://github.com/stomar/&gt;

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] Programming Ruby: The Pragmatic Programmer's Guide
[2] http://rxr.whitequark.org/mri/source/eval.c#714

···

On Tue, Dec 4, 2012 at 12:13 PM, Kai König <kai@kairichardkoenig.de> wrote:

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

OK, so apparently this has been answered on StackOverflow:

···

On Tue, Dec 4, 2012 at 5:01 PM, Eric Christopherson <echristopherson@gmail.com> wrote:

On Tue, Dec 4, 2012 at 12:13 PM, Kai König <kai@kairichardkoenig.de> wrote:

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

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] Programming Ruby: The Pragmatic Programmer's Guide
[2] http://rxr.whitequark.org/mri/source/eval.c#714