From: Damphyr Date: 2006-02-24T00:19:14+09:00 Subject: Re: FILE test David Vallner wrote: > D��a Utorok 21 Febru��r 2006 20:25 Damphyr nap��sal: >> Peter Hickman 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. >> > > 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.freemail.gr - ������ �������� ������������ ������������. http://www.freemail.gr - free email service for the Greek-speaking.