[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Finding path to ruby script argument

Matthew Hailstone

1/11/2007 6:30:00 PM

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

10 Answers

jgbailey

1/11/2007 6:33:00 PM

0

On 1/11/07, Matthew Hailstone <matthew.hailstone@gmail.com> 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

Philip Hallstrom

1/11/2007 6:36:00 PM

0

Matthew Hailstone

1/11/2007 6:42:00 PM

0

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/ruby... page.

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

Thanks,

Matthew

On 1/11/07, Justin Bailey <jgbailey@gmail.com> wrote:
> On 1/11/07, Matthew Hailstone <matthew.hailstone@gmail.com> 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

1/11/2007 6:45:00 PM

0

Excellent! Thanks.

On 1/11/07, Matthew Hailstone <matthew.hailstone@gmail.com> 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/ruby... page.
>
> Justin, how would you recommend me using the _FILE_ constant?
>
> Thanks,
>
> Matthew
>
> On 1/11/07, Justin Bailey <jgbailey@gmail.com> wrote:
> > On 1/11/07, Matthew Hailstone <matthew.hailstone@gmail.com> 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
> >
> >
>
>

Gavin Kistner

1/11/2007 6:45:00 PM

0

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

1/11/2007 8:21:00 PM

0

Looks like __FILE__ and $0 are the same.

Thanks for the clarification on __FILE__,

Matthew

On 1/11/07, Phrogz <gavin@refinery.com> 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.
>
>
>

Jano Svitok

1/11/2007 8:30:00 PM

0

On 1/11/07, Matthew Hailstone <matthew.hailstone@gmail.com> 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.

Philip Hallstrom

1/11/2007 8:40:00 PM

0

Matthew Hailstone

1/11/2007 9:04:00 PM

0

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

Christian Neukirchen

1/14/2007 11:45:00 AM

0

"Justin Bailey" <jgbailey@gmail.com> writes:

> On 1/11/07, Matthew Hailstone <matthew.hailstone@gmail.com> 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.

s/constant/keyword/

> Justin
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...