See the following example:
puts __FILE__ #test.rb
puts File.expand_path(__FILE__) #C:/TEMP/test.rb
Dir.chdir('..')
puts __FILE__ #test.rb
puts File.expand_path(__FILE__) #C:/test.rb
After a (global) chdir
the expand_path
returns a wrong result.
How can I get the correct result?
I tried to use the 2nd parameter of File.expand_path
:
puts File.expand_path(__FILE__, 'temp') #C:/TEMP/test.rb
puts File.expand_path(__FILE__, 'c:/temp') #C:/TEMP/test.rb
But to use it, I must know the path of __FILE__
.
The command require_relative
seems to ignore all chdir
-actions. So I have the hope, there is a way to get the 'real' directory of a file.
Remarks:
- I know the block-version of Dir.chdir - for my specific task i can#t use it.
- My actual solution: I store the fullpath before I change the directory (I could also store
Dir.pwd
before I change the directory).