mhinton
January 19, 2007, 4:31pm
#1
When I execute a ruby script by the following:
ruby helloworld.rb onlyarg
How can I find what 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
mhinton
September 25, 2007, 10:37pm
#3
Matthew H. 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.
mhinton
September 25, 2007, 10:38pm
#4
On 1/11/07, Matthew H. [email protected] wrote:
When I execute a ruby script by the following:
ruby helloworld.rb onlyarg
How can I find what equals?
The constant FILE will contain the full path to the currently
executing file.
Justin
mhinton
September 25, 2007, 10:44pm
#6
Looks like FILE and $0 are the same.
Thanks for the clarification on FILE ,
Matthew
mhinton
September 25, 2007, 10:38pm
#7
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
mhinton
September 25, 2007, 10:45pm
#8
“Justin B.” [email protected] writes:
On 1/11/07, Matthew H. [email protected] wrote:
When I execute a ruby script by the following:
ruby helloworld.rb onlyarg
How can I find what equals?
The constant FILE will contain the full path to the currently
executing file.
s/constant/keyword/
mhinton
September 25, 2007, 10:44pm
#9
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
.
onlyarg
C:/scripts/helloworld.rb
C:/scripts/helloworld.rb
includeme.rb
./includeme.rb
helloworld.rb
end includeme.rb
Looks like out of the originally invoked rb script, the whole path is
lost. I would have to assign the value to a global variable if I
needed to preserve it to all scripts.
Matthew
mhinton
September 25, 2007, 10:50pm
#10
On 1/11/07, Matthew H. [email protected] 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.
mhinton
September 25, 2007, 10:48pm
#11
When I execute a ruby script by the following:
ruby helloworld.rb onlyarg
How can I find what 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