Search code examples
rubyload-path

$LOAD_PATH does not include working directory?


Why isn't the current directory included in $LOAD_PATH? This seems odd. I just keep adding it, but is there some reason I am not seeing that it's just not automatically included?


Solution

  • $LOAD_PATH includes current directory in Ruby 1.8.7, but this behavior have been changed in 1.9.2. You can find possible explanations for the reasons behind this decision among answers to this question, but I think basic idea is that . in 1.8.7 stands for directory from which your code is executed and not the one where it is located. And in most cases you don't want that and . in your $LOAD_PATH is not reliable.

    Using require_relative in 1.9.2 might be a good solution if you don't want to add . manually to your $LOAD_PATH everywhere. You can see here that what it does is just explicit expanding relative path. One thing to note is that it's not available in versions prior to 1.9.2 so it'll make your code incompatible with older rubies.