Ran into this problem the other day. Thought I’d blog about it before it got too stale in my mind. In most typical entry points to an application you’ll see something like this:
p. Here it is adding “../lib” to the load path and then loading your-lib-name.rb from that directory. However, when this script is being run via a symlink, FILE will be interpretted as the location of the symlink. Obviously, because “../lib” is a relative location this can cause problems. This will give an error that I am sure most developers will be familiar with:
And absolute paths really aren’t a very good option either. So what to do? Well, luckily, you can do this:
As you can see, Pathname.realpath resolves the symlink to the real location. I thought it was an interesting problem and am surprised I haven’t seen this sooner.