Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 433
Blankman Avatar asked Mar 01 '11 14:03

Blankman


People also ask

What is file dirname?

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).

What does __ FILE __ mean in Ruby?

__FILE__ is the filename with extension of the file containing the code being executed. In foo. rb , __FILE__ would be "foo. rb".

What is $Load_path Ruby?

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.

What is __ DIR __ in Ruby?

__dir__ is a alias to a function As the ruby documentation says __dir__ is equal to File.dirname(File.realpath(__FILE__))


2 Answers

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.

like image 50
idlefingers Avatar answered Sep 20 '22 04:09

idlefingers


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"] 
like image 10
Wes Avatar answered Sep 20 '22 04:09

Wes