Search code examples
rubyload-path

In the Ruby programming language, what is the name of $:


I want to know more about $: but I don't how is called.

:015 > $:
=> ["/Users/Nerian/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/site_ruby/1.9.1", 
 "/Users/Nerian/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/site_ruby/1.9.1/x86_64-darwin11.1.0", 
 "/Users/Nerian/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/site_ruby", 
 "/Users/Nerian/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/vendor_ruby/1.9.1", 
 "/Users/Nerian/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin11.1.0", 
 "/Users/Nerian/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/vendor_ruby", 
 "/Users/Nerian/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/1.9.1", 
 "/Users/Nerian/.rvm/rubies/ruby-1.9.3-rc1/lib/ruby/1.9.1/x86_64-darwin11.1.0"]
  • What is the name of this?
  • How and when is it used?
  • Should it be used at all, is it a good practice or a bad practice?
  • Is it supported by all Ruby implementations?
  • Any docs about it?

Solution

    • The canonical (english) name of the $: global is $LOAD_PATH.
    • As the name says, it is an Array of library search paths, i.e. an array of Strings representing all the folders where the interpreter will search libraries in (when it encounters a require "mylibrary" instruction)
    • It can be used with the same caution that has to be paid when dealing with globals. Actually, it is often used when writing test or demo scripts included in gems or libraries, so that the test modifies the load path in order to find the library under test before installing it (e.g. $: << "../lib" assuming the script is in a sibling of lib)
    • It is used by all canonical Ruby versions/implementation. Note though that the current directory . was part of $: on 1.8.x, and has been removed for security reasons on 1.9.x.
    • Docs is available on every Ruby primer (picaxe book, ruby docs site).