Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "$" character mean in Ruby?

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?

like image 263
Scott Radcliff Avatar asked Dec 13 '09 15:12

Scott Radcliff


People also ask

What is character in Ruby?

chars is a String class method in Ruby which is used to return an array of characters in str. Syntax: str.chars. Parameters: Here, str is the given string. Returns: An array of the characters.

What does :: mean in Ruby?

The use of :: on the class name means that it is an absolute, top-level class; it will use the top-level class even if there is also a TwelveDaysSong class defined in whatever the current module is.

What does #{} mean in Ruby?

It's called String Interpolation. In Ruby, string interpolation refers to the ability of double-quoted strings to execute Ruby code and replace portions of that strings (denoted by #{ ... }) with the evaluation of that Ruby code.

What is the point of symbols in Ruby?

Ruby symbols are defined as “scalar value objects used as identifiers, mapping immutable strings to fixed internal values.” Essentially what this means is that symbols are immutable strings. In programming, an immutable object is something that cannot be changed.


1 Answers

$ 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

like image 113
Justin Love Avatar answered Oct 12 '22 21:10

Justin Love