Last Updated: March 22, 2016
·
1.599K
· Evgenia Karunus

Path (Linux), LOAD_PATH (Ruby) and autoload_paths (Rails).

Coming into Rails development, you may stumble upon 3 different paths:


Linux

[$PATH environmental variable]

~❯ echo $PATH
    /home/lakesare/.rvm/gems/ruby-2.2.1/bin:
    /usr/local/sbin:
    /usr/local/bin:
    /usr/sbin:
    /usr/bin:
    /sbin:
    /bin:...

is a : - divided list of absolute paths of directories with binaries, eg ls binary. which you can call without mentioning the absolute path. like

~❯ rails

and, if it works, there is a rails binary in one of those paths. in out case, in

~❯ whereis rails                                     
    rails: /home/lakesare/.rvm/gems/ruby-2.2.1/bin/rails

Ruby

[$LOAD_PATH global variable]

[1] irb(main)> $LOAD_PATH
    => ['/home/lakesare/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0', ...]

Rubygems overwrites Kernel.require. Rubygems gem is included in standard library since ruby 1.9.
When you call require 'a', this is what happens:

File can be loaded from the existing Ruby loadpath?

  • yes -> it is loaded.

  • no -> installed gems are searched for a file (in /lib) that matches. if it's found in gem 'b', that gem is added to the loadpath (activated).


Rails

[autoload_paths() rails method]

is used to autoload constants.

module A::B
  C # Module.nesting => [A::B]
end

when Ruby sees constant C, to resolve it, it looks in:

  1. Each entry in Module.nesting

  2. Each entry in Module.nesting.first.ancestors

  3. Each entry in Object.ancestors if Module.nesting.first is nil or a module.

when Rails sees constant C, to resolve it, Rails:

  1. looks in every place Ruby looks at.

  2. if it hasn't been found, calls A::B.const_missing("C"). Rails then:

    1. Looks within autoload_paths for a/b/c.rb
    2. If a matching file is found, it is loaded:

      1. If the correct constant is defined, it is returned
      2. Otherwise, an error is raised
    3. If no matching file is found, it looks instead for A::C, then C, unless they are already defined

    4. If none of the candidate constants can be loaded, it raises a NameError