# FILE test

**URL:** <https://www.ruby-forum.com/t/file-test/71294>\
**Category:** Ruby\
**Created:** [February 21, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/file-test/71294 "2006-02-21T15:59:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 21, 2006, 3:59pm UTC](https://www.ruby-forum.com/t/file-test/71294/1 "2006-02-21T15:59:22Z")

</div>

I noticed that a lot of scripts apply the following coding pattern:

if **FILE** == $0  
…  
end

I know what is does but what kind of problems does it solve?

thx,

used-to-be-a-Smalltalker

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 21, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/file-test/71294/2 "2006-02-21T16:17:54Z")

</div>

On Feb 21, 2006, at 8:58 AM, raving\_ruby\_rider wrote:

> I noticed that a lot of scripts apply the following coding pattern:
> 
> if **FILE** == $0  
> …  
> end
> 
> I know what is does but what kind of problems does it solve?

It allows the file to be both a library (when required the if  
statement will not run) and executable.

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 21, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/file-test/71294/3 "2006-02-21T16:24:02Z")

</div>

You will see this sort of thing in other scripting languages. **FILE**  
contains the name of the file source file and $0 is the name of the  
currently executing script. So a file called hello.rb

class Hello  
def initialize(name)  
@name = name  
end

```
def greet
    puts "Hello #{@name}"
end

```

end

if **FILE** == $0 then  
h = Hello.new(‘World’)  
h.greet  
end

you can then run this file with ruby hello.rb which will run the code at  
the bottom. However with tom.rb

require ‘hello’

h = Hello.new(‘tom’)  
h.greet

when you run tom.rb the **FILE** == $0 part of hello does not run as the  
file hello.rb but the currently executing script is tom.rb. This allows  
you to have a ruby file hold the class for inclusion by other scripts  
and also be a utility script in it’s own right.

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 22, 2006, 2:16am UTC](https://www.ruby-forum.com/t/file-test/71294/4 "2006-02-22T02:16:13Z")

</div>

DÅ?a Utorok 21 FebruÃ¡r 2006 20:25 Damphyr napÃ­sal:

> > and also be a utility script in it’s own right.
> 
> Not to mention the fact that it allows you to easily write unit tests  
> for most of the code in a script.

Which I personally can’t bear the sight of. Library is script is test?  
Nuh-uh.  
Just my two cents.

David V.

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 22, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/file-test/71294/5 "2006-02-22T21:01:26Z")

</div>

On 2/21/06, David V. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Which I personally can’t bear the sight of. Library is script is test? Nuh-uh.

So what is the ‘ruby way’ to store your unit tests? A separate  
require’d file?

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 21, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/file-test/71294/6 "2006-02-21T20:26:19Z")

</div>

Peter H. wrote:

> You will see this sort of thing in other scripting languages. **FILE**  
> contains the name of the file source file and $0 is the name of the  
> currently executing script. So a file called hello.rb

…

> when you run tom.rb the **FILE** == $0 part of hello does not run as the  
> file hello.rb but the currently executing script is tom.rb. This allows  
> you to have a ruby file hold the class for inclusion by other scripts  
> and also be a utility script in it’s own right.

Not to mention the fact that it allows you to easily write unit tests  
for most of the code in a script.  
V.-

–  
[http://www.braveworld.net/riva](http://www.braveworld.net/riva)

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 22, 2006, 10:13pm UTC](https://www.ruby-forum.com/t/file-test/71294/7 "2006-02-22T22:13:54Z")

</div>

Adam S. wrote:

> So what is the ‘ruby way’ to store your unit tests? A separate  
> require’d file?

Well, going by the Pickaxe book you end up with something like this:

./classfilename  
-\>/doc  
-\>/lib  
-\>/test

in ./classfilename/lib you create your ruby source file classfilename.rb  
in ./classfilename/test you create your ruby test/unit file  
classfilename\_tc.rb

In classfilename\_tc.rb you put the following lines at the start:

#----------------------------------------------------------------

# The following prefixes …/lib to the active ruby load path

$:.unshift File.join(File.dirname( **FILE** ), “…”, “lib”)

require ‘test/unit’  
require ‘classfilename’

class Test\_ClassFileName \< Test::Unit::TestCase  
…  
#----------------------------------------------------------------

and then write your test cases as methods. When you run your test suite  
you can invoke it from any palce on the system as the #unshift prefixes  
the load path with the relative location of the classfilename.rb with  
respect to the test case file.

Thus, assuming that for the example given above that ./ = ~/ruby then:

#ruby -w ~/ruby/classfilename/test/classfilename\_tc.rb

will work whatever pwd you are in.

I love test/unit…

Regards,  
Jim

---

<div class="post-metadata">

**Author:** ![raving\_ruby\_rider](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c6cbf5/32.png) [@raving\_ruby\_rider](https://www.ruby-forum.com/u/raving_ruby_rider)\
**Post date:** [February 23, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/file-test/71294/8 "2006-02-23T16:20:16Z")

</div>

David V. wrote:

> > > and also be a utility script in it’s own right.  
> > > Not to mention the fact that it allows you to easily write unit tests  
> > > for most of the code in a script.
> 
> Which I personally can’t bear the sight of. Library is script is test? Nuh-uh.  
> Just my two cents.  
> Well I actually put the unit tests in a different file.  
> The if $0== **FILE** check allows me to require the script in the unit  
> test file.  
> Following mostly the DRY principle and having a knack for organizing  
> code allows you to group most of the functionality in objects (at which  
> point you put them in a ‘library’ file and forget about it) or methods  
> to be used by the ‘top-level’ script. Requiring the ‘script’ file allows  
> me to unit test the methods without contriving manual tests.  
> I found it most valuable when I do parameter parsing and  
> parameter/configuration validation in my command line scripts.  
> Cheers,  
> V.-

–  
[http://www.braveworld.net/riva](http://www.braveworld.net/riva)
