Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean $: in Ruby

Tags:

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.

like image 952
Joe.wang Avatar asked Dec 06 '22 23:12

Joe.wang


2 Answers

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.

like image 162
Yu Hao Avatar answered Jan 11 '23 17:01

Yu Hao


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 > 
like image 31
Pramod Solanky Avatar answered Jan 11 '23 17:01

Pramod Solanky