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
The FILE_DIRNAME function returns the dirname of a file path. A file path is a string containing one or more segments consisting of names separated by directory delimiter characters (slash (/) under UNIX, or backslash (\) under Microsoft Windows).
__FILE__ is the filename with extension of the file containing the code being executed. In foo. rb , __FILE__ would be "foo. rb".
In ruby an identifier starting with a $ symbol is a global variable. $LOAD_PATH is an array of absolute paths i.e it stores the exact location of all the dependencies in the project.
__dir__ is a alias to a function As the ruby documentation says __dir__ is equal to File.dirname(File.realpath(__FILE__))
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With