# Same ruby file acting standalone resp. as library

**URL:** <https://www.ruby-forum.com/t/same-ruby-file-acting-standalone-resp-as-library/92631>\
**Category:** Ruby\
**Created:** [April 21, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/same-ruby-file-acting-standalone-resp-as-library/92631 "2007-04-21T22:25:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [April 21, 2007, 10:25pm UTC](https://www.ruby-forum.com/t/same-ruby-file-acting-standalone-resp-as-library/92631/1 "2007-04-21T22:25:56Z")

</div>

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 H.

---

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [April 21, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/same-ruby-file-acting-standalone-resp-as-library/92631/2 "2007-04-21T22:45:56Z")

</div>

On Saturday 21 April 2007 16:25, Thomas H. 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-talk/248563](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/248563)

---

<div class="post-metadata">

**Author:** ![samoht](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@samoht](https://www.ruby-forum.com/u/samoht)\
**Post date:** [April 22, 2007, 12:56am UTC](https://www.ruby-forum.com/t/same-ruby-file-acting-standalone-resp-as-library/92631/3 "2007-04-22T00:56:07Z")

</div>

Jesse M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote/schrieb  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

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

Yes, that’s it.

Thanks  
Thomas
