What is the "$ :. unshift File.dirname (__ FILE__)"?

What does the following do and why is it at the top of the page?

$:.unshift File.dirname(__FILE__) 

https://github.com/mojombo/jekyll/blob/master/lib/jekyll.rb

+45
ruby ruby-on-rails
Mar 01 2018-11-11T00:
source share
3 answers

It adds the current file directory to the download path. $: represents the load path (which is an array), and unshift added to the beginning of the array.

The reason that he (and above) is that all this requires, no need to worry about the path.

+51
Mar 01 2018-11-11T00:
source share

Technically, it adds the file path as the first entry of the download path that ruby ​​uses to search for files. $: is a magic variable and is more clearly denoted 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"] 
+7
Mar 02 2018-11-11T00:
source share

It adds the current working directory path to all the needs used in the project. After adding this from above, we don’t need to worry about the file path that we need, but the whole required file should be in the same directory where our main program requires other files.

$: reserved keyword for loading path.

+3
Apr 13 2018-12-12T00:
source share



All Articles