What is '$:.unshift File.dirname(__FILE__)' doing?
What is the following doing, and why is it at the top of the page?
$:.unshift File.dirname(__FILE__)
https://github.com/mojombo/jekyll/blob/master/lib/jekyll.rb
It's adding the current file's directory to the load path. $:
represents the load path (which is an array) and unshift
prepends to the beginning of the array.
The reason it's there (and at the top) is so that all those requires needn't worry about the path.
Technically it is adding the path of the file as the first entry of the load path that ruby uses to look for files. $: is a magic variable and more clearly referenced by $LOAD_PATH.
ruby-1.9.2-p136 > $LOAD_PATH
=> ["/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/x86_64-darwin10.6.0"]
ruby-1.9.2-p136 > $:
=> ["/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/x86_64-darwin10.6.0"]
ruby-1.9.2-p136 > $:.unshift '.'
=> [".", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/x86_64-darwin10.6.0", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin10.6.0", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/vendor_ruby", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1", "/Users/wesbailey/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/x86_64-darwin10.6.0"]
It add the current working directory path to all the require used in the project, After adding this on the top we don't need to bother about the file path we are requiring, but all the file we are requiring must be in the same directory where our main program requiring other files.
$: is reserved key word to load the path.