Forum: Ruby catch throw ?

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.
Fd870ca69f1243766bd8697ea3415df2?d=identicon&s=25 Paatsch, Bernd (Guest)
on 2006-03-09 02:37
(Received via mailing list)
Hello,

I just started using catch and throw. Therefore this question might be
an
easy one.

I have multiple ruby files. In one of the files I like to throw and then
in
the main function  in a different file I like to catch the throw. Is
this
possible or do throw/catch need to be in the same file?


Example:
fileOne.rb

def somefunct
....
throw :quit_script
....
end

fileTwo.rb
catch :quit_script
...
end

Thx,
Bernd
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-09 02:55
(Received via mailing list)
On Mar 8, 2006, at 8:35 PM, Paatsch, Bernd wrote:

> Is this
> end
>
> fileTwo.rb
> catch :quit_script
> ...
> end
>
> Thx,
> Bernd

They have to be in the same call chain.

so if fileTwo require's fileOne.rb and fileTwo's code looks like
catch :quit_script do
    ...
    somefunct()
   ...
end

it will work
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-09 04:08
(Received via mailing list)
On Thu, 9 Mar 2006, Paatsch, Bernd wrote:

>
> catch :quit_script
> ...
> end
>
> Thx,
> Bernd

     harp:~ > cat a.rb
     p catch('answer'){ load 'b.rb' }

     harp:~ > cat b.rb
     throw 'answer', 42

     harp:~ > ruby a.rb
     42

hth.

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