# Detect if required or executed?

**URL:** <https://www.ruby-forum.com/t/detect-if-required-or-executed/140707>\
**Category:** Ruby\
**Created:** [June 17, 2008, 8:01pm UTC](https://www.ruby-forum.com/t/detect-if-required-or-executed/140707 "2008-06-17T20:01:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISESSmith-Heister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ea666f/32.png) [@ISESSmith-Heister](https://www.ruby-forum.com/u/ISESSmith-Heister)\
**Post date:** [June 17, 2008, 8:01pm UTC](https://www.ruby-forum.com/t/detect-if-required-or-executed/140707/1 "2008-06-17T20:01:27Z")

</div>

Hi all,

Is anyone aware of a way to detect if a file has been required from  
another ruby process, or is being executed from the commandline? I’ve  
been doing this hack:

do\_executable\_behavior unless ENV[‘DONT\_RUN’]

but that’s clearly not ideal. I though maybe I could add something to  
the bangline? But that wouldn’t help if someone runs the script  
indirectly with “ruby script.rb”. Something like Module.included would  
be great if there was a “required” method, I could just set a DONT\_RUN  
variable there.

Thanks,  
Ian

---

<div class="post-metadata">

**Author:** ![ISESSmith-Heister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ea666f/32.png) [@ISESSmith-Heister](https://www.ruby-forum.com/u/ISESSmith-Heister)\
**Post date:** [June 17, 2008, 8:06pm UTC](https://www.ruby-forum.com/t/detect-if-required-or-executed/140707/2 "2008-06-17T20:06:13Z")

</div>

I. E. Smith-Heisters wrote:

> indirectly with “ruby script.rb”. Something like Module.included would  
> be great if there was a “required” method, I could just set a DONT\_RUN  
> variable there.
> 
> Thanks,  
> Ian

There was a good discussion recently about making sure only one instance  
of a script is running, maybe that helps:

[http://groups.google.com/group/ruby-talk-google/browse\_thread/thread/b658d9b0dbfd59f8](http://groups.google.com/group/ruby-talk-google/browse_thread/thread/b658d9b0dbfd59f8)

---

<div class="post-metadata">

**Author:** ![ISESSmith-Heister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ea666f/32.png) [@ISESSmith-Heister](https://www.ruby-forum.com/u/ISESSmith-Heister)\
**Post date:** [June 17, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/detect-if-required-or-executed/140707/3 "2008-06-17T20:09:13Z")

</div>

On Jun 17, 2008, at 11:59 AM, I. E. Smith-Heisters wrote:

> Is anyone aware of a way to detect if a file has been required from  
> another ruby process, or is being executed from the commandline? I’ve  
> been doing this hack:

executed = **FILE** == $0  
required = not executed

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![ISESSmith-Heister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ea666f/32.png) [@ISESSmith-Heister](https://www.ruby-forum.com/u/ISESSmith-Heister)\
**Post date:** [June 17, 2008, 11:35pm UTC](https://www.ruby-forum.com/t/detect-if-required-or-executed/140707/4 "2008-06-17T23:35:49Z")

</div>

On Jun 17, 11:07 am, “ara.t.howard” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> we can deny everything, except that we have the possibility of being  
> better. simply reflect on that.  
> h.h. the 14th dalai lama

clever, thanks!

---

<div class="post-metadata">

**Author:** ![ISESSmith-Heister](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ea666f/32.png) [@ISESSmith-Heister](https://www.ruby-forum.com/u/ISESSmith-Heister)\
**Post date:** [June 17, 2008, 8:18pm UTC](https://www.ruby-forum.com/t/detect-if-required-or-executed/140707/5 "2008-06-17T20:18:04Z")

</div>

Joel VanderWerf wrote:

> There was a good discussion recently about making sure only one instance  
> of a script is running, maybe that helps:
> 
> [http://groups.google.com/group/ruby-talk-google/browse\_thread/thread/b658d9b0dbfd59f8](http://groups.google.com/group/ruby-talk-google/browse_thread/thread/b658d9b0dbfd59f8)

Oops, I think Ara answered the right question…
