Joe.wang
Joe.wang

Reputation: 11791

What does it mean $: in Ruby

I was reading the following tutorial.

It talked about including files in a Ruby file like require :

require(string) => true or false

Ruby tries to load the library named string, returning true if successful. If the filename does not resolve to an absolute path, it will be searched for in the directories listed in $:. If the file has the extension ".rb", it is loaded as a source 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 adding ".rb", ".so", and so on to the name. The name of the loaded feature is added to the array in $:.

I just want to know what is $: in Ruby and what does $: means.

Upvotes: 5

Views: 977

Answers (3)

Darkmouse
Darkmouse

Reputation: 1939

In ruby $ refers to a predefined variable.

In this case, $: is short-hand for $LOAD_PATH. This is the list of directories you can require files from while giving a relative path. In other words, Ruby searches the directories listed in $:

Hope this helps.

Upvotes: 2

Pramod Solanky
Pramod Solanky

Reputation: 1700

Its the load path

Just open in irb terminal and type this $: This is what you would get. Ofcourse that depends on the ruby ur using.

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 > 

Upvotes: 3

Yu Hao
Yu Hao

Reputation: 122403

The variable $: is one of the execution environment variables, which is an array of places to search for loaded files.

The initial value is the value of the arguments passed via the -I command-line option, followed by an installation-defined standard library location.

See Pre-defined variables, $LOAD_PATH is its alias.

Upvotes: 11

Related Questions