Finding path to ruby script argument

Discussion in 'Ruby' started by Matthew Hailstone, Jan 11, 2007.

  1. When I execute a ruby script by the following:

    ruby <path>helloworld.rb onlyarg

    How can I find what <path> equals?

    For example, in Windows, ruby C:\scripts\helloworld.rb onlyarg

    helloworld.rb:
    ----------------------------------------------
    # What can I put here to display the value C:\scripts\ or C:\scripts?
    puts ARGV[0]
    ----------------------------------------------

    output:
    ----------------------------------------------
    onlyarg
    ----------------------------------------------


    Thanks!

    Matthew
    Matthew Hailstone, Jan 11, 2007
    #1
    1. Advertising

  2. On 1/11/07, Matthew Hailstone <> wrote:
    > When I execute a ruby script by the following:
    >
    > ruby <path>helloworld.rb onlyarg
    >
    > How can I find what <path> equals?


    The constant __FILE__ will contain the full path to the currently
    executing file.

    Justin
    Justin Bailey, Jan 11, 2007
    #2
    1. Advertising

  3. > When I execute a ruby script by the following:
    >
    > ruby <path>helloworld.rb onlyarg
    >
    > How can I find what <path> equals?


    $0 will give you the full string... then use chop it up using dirname to
    get the directory...

    % cat foo.rb
    puts File.dirname($0)
    % ruby /Users/philip/foo.rb
    /Users/philip

    >
    > For example, in Windows, ruby C:\scripts\helloworld.rb onlyarg
    >
    > helloworld.rb:
    > ----------------------------------------------
    > # What can I put here to display the value C:\scripts\ or C:\scripts?
    > puts ARGV[0]
    > ----------------------------------------------
    >
    > output:
    > ----------------------------------------------
    > onlyarg
    > ----------------------------------------------
    >
    >
    > Thanks!
    >
    > Matthew
    >
    >
    Philip Hallstrom, Jan 11, 2007
    #3
  4. ruby C:\scripts\helloworld.rb

    helloworld.rb:
    ----------------------------------------------
    # What can I put here to display the value C:\scripts\ or C:\scripts?
    puts $0
    puts _FILE_
    puts ARGV[0]
    ----------------------------------------------

    output:
    ----------------------------------------------
    C:/scripts/helloworld.rb
    C:/scripts/helloworld.rb:3: undefined local variable or method
    `_FILE_' for main:Object (NameError)
    ----------------------------------------------


    ruby C:\scripts\helloworld.rb

    helloworld.rb:
    ----------------------------------------------
    # What can I put here to display the value C:\scripts\ or C:\scripts?
    puts $0
    puts ARGV[0]
    ----------------------------------------------

    output:
    ----------------------------------------------
    C:/scripts/helloworld.rb
    onlyarg

    ----------------------------------------------

    I found the $0 documented in the
    http://www.rubycentral.com/book/rubyworld.html page.

    Justin, how would you recommend me using the _FILE_ constant?

    Thanks,

    Matthew

    On 1/11/07, Justin Bailey <> wrote:
    > On 1/11/07, Matthew Hailstone <> wrote:
    > > When I execute a ruby script by the following:
    > >
    > > ruby <path>helloworld.rb onlyarg
    > >
    > > How can I find what <path> equals?

    >
    > The constant __FILE__ will contain the full path to the currently
    > executing file.
    >
    > Justin
    >
    >
    Matthew Hailstone, Jan 11, 2007
    #4
  5. Excellent! Thanks.

    On 1/11/07, Matthew Hailstone <> wrote:
    > ruby C:\scripts\helloworld.rb
    >
    > helloworld.rb:
    > ----------------------------------------------
    > # What can I put here to display the value C:\scripts\ or C:\scripts?
    > puts $0
    > puts _FILE_
    > puts ARGV[0]
    > ----------------------------------------------
    >
    > output:
    > ----------------------------------------------
    > C:/scripts/helloworld.rb
    > C:/scripts/helloworld.rb:3: undefined local variable or method
    > `_FILE_' for main:Object (NameError)
    > ----------------------------------------------
    >
    >
    > ruby C:\scripts\helloworld.rb
    >
    > helloworld.rb:
    > ----------------------------------------------
    > # What can I put here to display the value C:\scripts\ or C:\scripts?
    > puts $0
    > puts ARGV[0]
    > ----------------------------------------------
    >
    > output:
    > ----------------------------------------------
    > C:/scripts/helloworld.rb
    > onlyarg
    >
    > ----------------------------------------------
    >
    > I found the $0 documented in the
    > http://www.rubycentral.com/book/rubyworld.html page.
    >
    > Justin, how would you recommend me using the _FILE_ constant?
    >
    > Thanks,
    >
    > Matthew
    >
    > On 1/11/07, Justin Bailey <> wrote:
    > > On 1/11/07, Matthew Hailstone <> wrote:
    > > > When I execute a ruby script by the following:
    > > >
    > > > ruby <path>helloworld.rb onlyarg
    > > >
    > > > How can I find what <path> equals?

    > >
    > > The constant __FILE__ will contain the full path to the currently
    > > executing file.
    > >
    > > Justin
    > >
    > >

    >
    >
    Matthew Hailstone, Jan 11, 2007
    #5
  6. Matthew Hailstone

    Phrogz Guest

    Matthew Hailstone wrote:
    > ruby C:\scripts\helloworld.rb
    >
    > helloworld.rb:
    > ----------------------------------------------
    > # What can I put here to display the value C:\scripts\ or C:\scripts?
    > puts $0
    > puts _FILE_


    There should be two underscores on each side of __FILE__, as the
    original poster had it.
    Phrogz, Jan 11, 2007
    #6
  7. Looks like __FILE__ and $0 are the same.

    Thanks for the clarification on __FILE__,

    Matthew

    On 1/11/07, Phrogz <> wrote:
    > Matthew Hailstone wrote:
    > > ruby C:\scripts\helloworld.rb
    > >
    > > helloworld.rb:
    > > ----------------------------------------------
    > > # What can I put here to display the value C:\scripts\ or C:\scripts?
    > > puts $0
    > > puts _FILE_

    >
    > There should be two underscores on each side of __FILE__, as the
    > original poster had it.
    >
    >
    >
    Matthew Hailstone, Jan 11, 2007
    #7
  8. Matthew Hailstone

    Jan Svitok Guest

    On 1/11/07, Matthew Hailstone <> wrote:
    > Looks like __FILE__ and $0 are the same.


    Not necessarily... when running a script under rcov or similar, they
    may differ (for example one starts with ./ while the other does not).
    That's why I write the if __FILE__ == $0 idiom as

    if File.expand_path(__FILE__) == File.expand_path($0)

    Another possibility might be is when you start the script using $PATH,
    i.e. not from current directory, but without specifying its path.
    Jan Svitok, Jan 11, 2007
    #8
  9. > On 1/11/07, Matthew Hailstone <> wrote:
    >> Looks like __FILE__ and $0 are the same.

    >
    > Not necessarily... when running a script under rcov or similar, they
    > may differ (for example one starts with ./ while the other does not).
    > That's why I write the if __FILE__ == $0 idiom as
    >
    > if File.expand_path(__FILE__) == File.expand_path($0)
    >
    > Another possibility might be is when you start the script using $PATH,
    > i.e. not from current directory, but without specifying its path.


    Also, and i haven't even bothered to test this, but if it's similar to
    PHP's FILE variable, it's the *current* file... so require a file, call a
    method in that file, and access __FILE__ in that method and you're gonna
    get your required file, not the initial one...

    -philip
    Philip Hallstrom, Jan 11, 2007
    #9
  10. helloworld.rb:
    ------------------------------
    puts $0
    puts __FILE__
    puts File.dirname($0)
    puts ARGV[0]
    puts File.expand_path(__FILE__)
    puts File.expand_path($0)

    require 'includeme.rb'

    tempvar = IncludeMe.new
    tempvar.runme
    ------------------------------

    includeme.rb:
    ------------------------------
    class IncludeMe
    def runme
    puts "includeme.rb"
    puts __FILE__
    puts $0
    puts "end includeme.rb"
    end
    end
    ------------------------------

    command:
    ------------------------------
    ruby helloworld.rb onlyarg
    ------------------------------

    output:
    ------------------------------
    helloworld.rb
    helloworld.rb
    Matthew Hailstone, Jan 11, 2007
    #10
    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. Bhushit Joshipura

    defaulting argument to previous argument

    Bhushit Joshipura, Dec 29, 2003, in forum: C++
    Replies:
    5
    Views:
    401
  2. Ben Kial
    Replies:
    1
    Views:
    633
    Eric Enright
    Nov 15, 2004
  3. S?ren Gammelmark
    Replies:
    1
    Views:
    1,867
    Eric Sosman
    Jan 7, 2005
  4. nw
    Replies:
    0
    Views:
    302
  5. Reckoner
    Replies:
    11
    Views:
    689
    Steven D'Aprano
    Jan 19, 2009
Loading...

Share This Page