What does $ mean: in Ruby

I was reading the following tutorial .

It talked about including files in a file Ruby

, for example require

:

require(string)

=> true

orfalse

Ruby tries to load a library named string, returning an true

if successful. If the file name is not resolved to an absolute path, it will look in the directories listed in $:

. If the file has a ".rb" extension, it is loaded as the original file; if the extension is ".so", ".o", or ".dll", or whatever the default shared library extension is on the current platform, Ruby loads the shared library as a Ruby extension. Otherwise Ruby tries to add ".rb", ".so", etc. addressed. The loaded function name is appended to the array in $:

.

I just want to know what is $:

in Ruby and what it means $:

.

+3


source to share


3 answers


The variable $:

is one of the runtime variables, which is an array of locations to search for downloaded files.

The initial value is the value of the arguments passed using the command line parameter -I

, followed by a standard library location.



See Predefined Variables , $LOAD_PATH

this is its alias.

+8


source


Its loading path

Just open irb terminal and type this $:

This is what you get. Of course it depends on the use of ruby ​​ur.



2.1.1 :009 > $:
=> ["/Users/mac/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0", "/Users/mac/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby/2.1.0/x86_64-darwin12.0", "/Users/mac/.rvm/rubies/ruby-2.1.1/lib/ruby/site_ruby", "/Users/mac/.rvm/rubies/ruby-2.1.1/lib/ruby/vendor_ruby/2.1.0", "/Users/mac/.rvm/rubies/ruby-2.1.1/lib/ruby/vendor_ruby/2.1.0/x86_64-darwin12.0", "/Users/mac/.rvm/rubies/ruby-2.1.1/lib/ruby/vendor_ruby", "/Users/mac/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0", "/Users/mac/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/x86_64-darwin12.0"] 
2.1.1 :010 > 

      

+3


source


The ruby $ refers to the predefined variable.

In this case, $: is short for $ LOAD_PATH. This is a list of directories from which you can request files by specifying a relative path. In other words, Ruby looks for directories listed in $:

Hope it helps.

+1


source







All Articles