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.readlinkto resolve a symlink but you’ll want to checkFile.symlink?first.Then you can work with
pathinstead of__FILE__. You might want to use$0instead of__FILE__as well,__FILE__is the current filename whereas$0is the name of the current script.