What does the "$" character mean in Ruby?

Scott Radcliff picture Scott Radcliff · Dec 13, 2009 · Viewed 35.5k times · Source

Been playing with Ruby on Rails for awhile and decided to take a look through the actual source. Grabbed the repo from GitHub and started looking around. Came across some code that I am not sure what it does or what it references.

I saw this code in actionmailer/test/abstract_unit.rb

root = File.expand_path('../../..', __FILE__)
 begin
 require "#{root}/vendor/gems/environment"
 rescue LoadError
 $:.unshift("#{root}/activesupport/lib")
 $:.unshift("#{root}/actionpack/lib")
end

lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib)

require 'rubygems'
require 'test/unit'

require 'action_mailer'
require 'action_mailer/test_case'

Can someone tell me what the $: (a.k.a. "the bling") is referencing?

Answer

Justin Love picture Justin Love · Dec 13, 2009

$ identifies a global variable, as opposed to a local variable, @instance variable, or @@class variable.

Among the language-supplied global variables are $:, which is also identified by $LOAD_PATH