Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Finding path to ruby script argument

Reply
Thread Tools

Finding path to ruby script argument

 
 
Matthew Hailstone
Guest
Posts: n/a
 
      01-11-2007
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

 
Reply With Quote
 
 
 
 
Justin Bailey
Guest
Posts: n/a
 
      01-11-2007
On 1/11/07, Matthew Hailstone <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Philip Hallstrom
Guest
Posts: n/a
 
      01-11-2007
> 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
>
>


 
Reply With Quote
 
Matthew Hailstone
Guest
Posts: n/a
 
      01-11-2007
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 <(E-Mail Removed)> wrote:
> On 1/11/07, Matthew Hailstone <(E-Mail Removed)> 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
>
>


 
Reply With Quote
 
Matthew Hailstone
Guest
Posts: n/a
 
      01-11-2007
Excellent! Thanks.

On 1/11/07, Matthew Hailstone <(E-Mail Removed)> 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 <(E-Mail Removed)> wrote:
> > On 1/11/07, Matthew Hailstone <(E-Mail Removed)> 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
> >
> >

>
>


 
Reply With Quote
 
Phrogz
Guest
Posts: n/a
 
      01-11-2007
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.

 
Reply With Quote
 
Matthew Hailstone
Guest
Posts: n/a
 
      01-11-2007
Looks like __FILE__ and $0 are the same.

Thanks for the clarification on __FILE__,

Matthew

On 1/11/07, Phrogz <(E-Mail Removed)> 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.
>
>
>


 
Reply With Quote
 
Jan Svitok
Guest
Posts: n/a
 
      01-11-2007
On 1/11/07, Matthew Hailstone <(E-Mail Removed)> 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.

 
Reply With Quote
 
Philip Hallstrom
Guest
Posts: n/a
 
      01-11-2007
> On 1/11/07, Matthew Hailstone <(E-Mail Removed)> 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

 
Reply With Quote
 
Matthew Hailstone
Guest
Posts: n/a
 
      01-11-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function argument dependent on another function argument? Reckoner Python 11 01-19-2009 03:31 AM
Template argument as template argument nw C++ 0 04-14-2008 01:36 PM
Function pointers, variable argument functions calling other variable-argument functions (sort of) S?ren Gammelmark C Programming 1 01-07-2005 09:41 PM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
defaulting argument to previous argument Bhushit Joshipura C++ 5 12-30-2003 03:21 PM



Advertisments