[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

same ruby file acting standalone resp. as library

Thomas Hafner

4/21/2007 8:18:00 PM

Hello,

the class definitions of my ruby file shall always be active, whether
the file is run as standalone program or whether the file is used
indirectly by require or load,
*but* when the file is run standalone, a special piece of code within
the file shall be active in addition (a kind of main section).

The idea is that the same ruby code file should be able to act in both
kinds: as a library and as a standalone program which is using that
library functionality.

How can I achieve that easily?

Regards
Thomas Hafner
2 Answers

Jesse Merriman

4/21/2007 8:45:00 PM

0

On Saturday 21 April 2007 16:25, Thomas Hafner wrote:
> the file is run as standalone program or whether the file is used
> indirectly by require or load,
> *but* when the file is run standalone, a special piece of code within
> the file shall be active in addition (a kind of main section).

Perhaps you want:

if __FILE__ == $0
(do something)
end

as was just mentioned in another thread:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...


--
Jesse Merriman
jessemerriman@warpmail.net
http://www.jessemer...

Thomas Hafner

4/21/2007 10:43:00 PM

0

Jesse Merriman <jesse.d.merriman@gmail.com> wrote/schrieb <200704211644.57610.jesse.d.merriman@gmail.com>:

> Perhaps you want:
>
> if __FILE__ == $0
> (do something)
> end

Yes, that's it.

Thanks
Thomas