How to access Bar in Foo when Foo::Bar exists?

W

Wejn

Hi,
I can't figure out how to:

class Foo
class Bar << RuntimeError; end

def something
begin
# ...
rescue Bar # i want Bar, not Foo::Bar
raise Bar # i want Foo::Bar, correct.
end
end
end

Does anyone have idea how to do that?
(IIRC, In c++ this is done via "::" operator)

Best regards,
Michal
 
J

Joel VanderWerf

Wejn said:
Hi,
I can't figure out how to:

class Foo
class Bar << RuntimeError; end

def something
begin
# ...
rescue Bar # i want Bar, not Foo::Bar
raise Bar # i want Foo::Bar, correct.
end
end
end

Does anyone have idea how to do that?
(IIRC, In c++ this is done via "::" operator)

(trying to avoid puns about raising the bar...)

Ruby also has a :: operator:

begin
class Bar < SyntaxError; end
class Foo
class Bar < RuntimeError; end

def something
begin
yield
rescue ::Bar # i want Bar, not Foo::Bar
raise Bar # i want Foo::Bar, correct.
end
end
end
Foo.new.something do raise Bar; end
rescue Exception => e
p e.class # => Foo::Bar
end
 
M

Michal

Ick!

1000.times { puts "I will think about the problem (and experiment in" +
" irb) before asking dumb questions." }

Thanks a lot :)

Michal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top