I have a Ruby script with path /foo/bar/gazook/script.rb
. I also created a symlink to it in $HOME/bin
.
Now, I want my Ruby script to access some other file in directory /foo
, and to keep paths relative, I have a variable FOO_DIRECTORY = File.expand_path(File.dirname(__FILE__) + "/../../")
in my script.
The problem is that if I run my script from its symlink, this relative directory is wrong (since I guess its expanding from a different location).
How do I fix this? Is there a way besides using an absolute path?
You can use File.readlink
to resolve a symlink but you'll want to check File.symlink?
first.
path = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
Then you can work with path
instead of __FILE__
. You might want to use $0
instead of __FILE__
as well, __FILE__
is the current filename whereas $0
is the name of the current script.
To get any path relative to the location of your script, always use __dir__
.
__dir__
is a concise way of saying File.dirname(File.realpath(__FILE__))
. It's available in Ruby >= 2.0. On __dir__
.
File.realpath(__FILE__)
(or Pathname#realpath
) has three advantages compared to File.readlink
:
readlink
only expands paths that are the last part of the argument.readlink
resolves only the first level.path
is a symlink at all. Thus you can drop the if File.symlink?
.Consequently it would be good to use FOO_DIRECTORY = File.join(__dir__, '..', '..')
or FOO_DIRECTORY = File.dirname(File.dirname(__dir__))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With