1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Predefined by Ruby


Build In Global Variables

In many languages, there are a number of built in constant variables. These variables store the state of some function or method or system, and are accessible globally. While many of these are not in general use today, some (such as the load path) are rather unavoidable.

Why aren't these in use today? First off, they're not thread safe. If you have one thread modify the state of this variable while another is trying to access it, bad things are going to happen. While threads are not a huge deal in Ruby, it's still something to think about. They're mostly just a throwback to Ruby's Perl-inspired roots.

  • $! - When an exception is raised, the object raised gets assigned to this variable. You can use this instead of explicitly using a variable in the rescue statement.
  • $@ - When an exception is raised, it also generates a stack backtrace. A stack backtrace walks backwards through the call stack and gives you a method call chain of how the program got to its current state.
  • $/ - This is the input record separator. By default this is a newline, but you can change it to any character you like. This is where methods such as gets look to know when a line ends.
  • $\ - This is the output record separator. By default this is nil, but this variable will be printed after every call to things like IO.write.
  • $, - This is the output field separator. This is printed between multiple arguments to methods calls like IO.write.
  • $; - This is the default string used by String#split.
  • $> - This is the IO object that methods like print and puts will write to. This is normally STDOUT, but you can change it here.
  • $_ - This should look familiar to all you Perl programmers out there. However, this is not used so much in Ruby (as it was way, way overused in Perl), as it's only used to temporarily hold the last line read by methods like gets.
  • $& - The string matched by a successful regular expression match.
  • $+ - The highest numbered capture group of a successful regular expression match.
  • $' - The section of a string before a successful regular expression match.
  • $` - The section of a string after a successful regular expression match.
  • $1..$x - The numbered capture groups of a successful regular expression match.
  • $0 - The name of the Ruby script being executed. This can be useful to determine if a script is being called directly, or the file has been required by another program.
  • $* - This is equivalent to ARGV, an array containing the command line arguments.
  • $" - This is another sometimes-useful one, it's an array of all the files required by the require method.
  • $$ - The PID (or "process ID") of the Ruby interpreter running this program.
  • $? - The exit status of the last child process run. This can be particularly useful for knowing if a program run with backticks completed successfully.
  • $: - Also known as $LOAD_PATH, this is perhaps the most directly used global variable. This is an array of strings with directories the require method can find Ruby files in.
  • $DEBUG - This one has no set purpose, but it is left open for the user. This is set to true if the command line -d is passed to the Ruby interpreter. This is particularly useful for printing debug messages only when the -d switch is passed.
  • $SAFE - The current safe level. This is a bit of a strange one, and can only be set to higher numbers, never lower numbers.
  1. About.com
  2. Technology
  3. Ruby
  4. Beginning Ruby
  5. Build In Global Variables

©2014 About.com. All rights reserved.