Forum: Ruby same ruby file acting standalone resp. as library

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.
8029153bbcbda4a6844440c93e0c6422?d=identicon&s=25 Thomas Hafner (Guest)
on 2007-04-21 22:25
(Received via mailing list)
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
B63d3390f6285b1af0f1662687911a95?d=identicon&s=25 Jesse Merriman (Guest)
on 2007-04-21 22:45
(Received via mailing list)
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/...
8029153bbcbda4a6844440c93e0c6422?d=identicon&s=25 Thomas Hafner (Guest)
on 2007-04-22 00:56
(Received via mailing list)
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
This topic is locked and can not be replied to.