FILE test

Discussion in 'Ruby' started by raving_ruby_rider, Feb 21, 2006.

  1. 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
     
    raving_ruby_rider, Feb 21, 2006
    #1
    1. Advertising

  2. 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 Gray II
     
    James Edward Gray II, Feb 21, 2006
    #2
    1. Advertising

  3. 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.
     
    Peter Hickman, Feb 21, 2006
    #3
  4. raving_ruby_rider

    Damphyr Guest

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

    --
    http://www.braveworld.net/riva

    ____________________________________________________________________
    http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
    http://www.freemail.gr - free email service for the Greek-speaking.
     
    Damphyr, Feb 21, 2006
    #4
  5. D=C5=88a Utorok 21 Febru=C3=A1r 2006 20:25 Damphyr nap=C3=ADsal:
    > 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__ =3D=3D $0 part of hello does not run a=

    s 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.=20
    Just my two cents.

    David Vallner
     
    David Vallner, Feb 22, 2006
    #5
  6. raving_ruby_rider

    Adam Shelly Guest

    On 2/21/06, David Vallner <> wrote:
    > Which I personally can't bear the sight of. Library is script is test? Nu=

    h-uh.

    So what is the 'ruby way' to store your unit tests? A separate require'd f=
    ile?
     
    Adam Shelly, Feb 22, 2006
    #6
  7. raving_ruby_rider

    James Byrne Guest

    Adam Shelly 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

    --
    Posted via http://www.ruby-forum.com/.
     
    James Byrne, Feb 22, 2006
    #7
  8. raving_ruby_rider

    Damphyr Guest

    David Vallner wrote:
    > D=C5=88a Utorok 21 Febru=C3=A1r 2006 20:25 Damphyr nap=C3=ADsal:
    >> 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__ =3D=3D $0 part of hello does not run=

    as the
    >>> file hello.rb but the currently executing script is tom.rb. This allo=

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

    >=20
    > Which I personally can't bear the sight of. Library is script is test? =

    Nuh-uh.=20
    > Just my two cents.

    Well I actually put the unit tests in a different file.
    The if $0=3D=3D__FILE__ check allows me to require the script in the unit=
    =20
    test file.
    Following mostly the DRY principle and having a knack for organizing=20
    code allows you to group most of the functionality in objects (at which=20
    point you put them in a 'library' file and forget about it) or methods=20
    to be used by the 'top-level' script. Requiring the 'script' file allows=20
    me to unit test the methods without contriving manual tests.
    I found it most valuable when I do parameter parsing and=20
    parameter/configuration validation in my command line scripts.
    Cheers,
    V.-

    --=20
    http://www.braveworld.net/riva

    ____________________________________________________________________
    http://www.freemail.gr - =E4=F9=F1=E5=DC=ED =F5=F0=E7=F1=E5=F3=DF=E1 =E7=EB=
    =E5=EA=F4=F1=EF=ED=E9=EA=EF=FD =F4=E1=F7=F5=E4=F1=EF=EC=E5=DF=EF=F5.
    http://www.freemail.gr - free email service for the Greek-speaking.
     
    Damphyr, Feb 23, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sting
    Replies:
    5
    Views:
    1,637
    Natty Gur
    Jun 8, 2004
  2. Robert Allan Schwartz
    Replies:
    7
    Views:
    384
    Marco Manfredini
    Aug 13, 2004
  3. Chris
    Replies:
    1
    Views:
    673
    Chris
    May 11, 2006
  4. Skybuck Flying

    Call oddities: &Test() vs &Test vs Test

    Skybuck Flying, Oct 4, 2009, in forum: C Programming
    Replies:
    1
    Views:
    726
    Skybuck Flying
    Oct 4, 2009
  5. Hunt Jon
    Replies:
    1
    Views:
    118
    Patrick Doyle
    Dec 15, 2008
Loading...

Share This Page