I have written some small Ruby scripts for system administration using Ruby 1.9.3. In one script, I use:
File.dirname(__FILE__)
to get the directory of the script file. This returns the relative path, however, when I call the script from the second script File.dirname , the absolute path is returned.
The Ruby Doc lists the absolute return path in my example, whereas I found a discussion on the Ruby Forum where the user says that dirname should only return a relative path.
I use the proposed solution from the Ruby Forums to use File.expand_path to always get the absolute path as follows:
File.expand_path(File.dirname(__FILE__))
but is there a way to make dirname behavior consistent?
UPDATE:
To extend the answer to Janathan Cairs, I made two scenarios:
s1.rb:
puts "External script __FILE__: #{File.dirname(__FILE__)}"
s0.rb:
puts "Local script __FILE__: #{File.dirname(__FILE__)}" require './s1.rb'
Running. / S0.rb gives the following result:
Local script __FILE__: . External script __FILE__: /home/kenneth/Pictures/wp/rip_vault
source share