[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Wejn

11/29/2003 12:58:00 AM

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
--
# Michal Safranek, email:
a=(("a".."z").to_a+["@","."]);p(("%b"%[0x645bbb83a6a496]
).scan(/...../).map{|x|a[Integer("0b"+x)]}.join.reverse)


2 Answers

Joel VanderWerf

11/29/2003 4:17:00 AM

0

Wejn wrote:
> 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



Wejn

11/29/2003 3:22:00 PM

0

Ick!

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

Thanks a lot :-)

Michal
--
# Michal Safranek, email:
a=(("a".."z").to_a+["@","."]);p(("%b"%[0x645bbb83a6a496]
).scan(/...../).map{|x|a[Integer("0b"+x)]}.join.reverse)