Under the hood: Ruby’s global variables

Tinkering in irb earlier, I ran this line of code.

global_variables.each{ |var| puts var + ": " + eval(var).inspect }

Ruby’s global variables - I have long been aware of their existence in the background, but I hadn’t realised quite how many are initialized by default. What are they all for - and what cool metaprogramming tricks can I use them for?

I actually use one of these variables very often, and it deserves a special mention. $! points to the most recently raised exception, and is a useful shortcut in many contexts. A lot of the others promise to be most useful in a shell/CGI script environment - but it’s good to know they’re there, and a little peek under the hood of Ruby usually proves instructional.

Here’s the global variables initialized by default on my system (Ruby 1.8.6 on OSX 10.5) - where I have some idea what they refer to, I’ve added that too.

Variable name Value (on my machine) Purpose?
$0 “irb” Name of current script executing
$” [”e2mmap.rb”, “irb/init.rb”, “irb/workspace.rb”, “irb/context.rb”, “irb/extend-command.rb”, “irb/output-method.rb”, “irb/notifier.rb”, “irb/slex.rb”, “irb/ruby-token.rb”, “irb/ruby-lex.rb”, “readline.bundle”, “irb/input-method.rb”, “irb/locale.rb”, “irb.rb”] Alias for $LOADED_FEATURES
$$ 6100 Ruby interpreter process ID
$< ARGF Points to ARGF (user input)
$_ nil Last string read by ‘gets’
$-K “NONE” $KCODE option passed via command line
$DEBUG false Can be set to true with ruby –debug myscript.rb. Just for program use I think?
$-i nil See ruby command line options: in-place-edit mode
$deferr # Usually points to $stderr
$/ “\n” Input record separator
$’ nil Text after regexp match
$stdout # Standard output (eg, the console usually)
$-l false See ruby command line options: automatic line-ending processing
$LOAD_PATH [”/usr/local/lib/ruby/site_ruby/1.8″, “/usr/local/lib/ruby/site_ruby/1.8/i686-darwin9.0.0″, “/usr/local/lib/ruby/site_ruby”, “/usr/local/lib/ruby/1.8″, “/usr/local/lib/ruby/1.8/i686-darwin9.0.0″, “.”]
$. 184 Last line of script read by interpreter
$KCODE “NONE” KanjiCODE - determines how Ruby handles characters
$-w false See Ruby intepreter command line options - sets $VERBOSE to true
$FILENAME “-” ?
$defout # Usually points to $stdout
$, nil ?
$` nil Text before regexp match
$* [] Command line arguments passed to current script
$stdin # Standard user input (usually, console)
$-F nil Field separator passed via command line
$-p false Used for command line file processing/looping (See also: -n)
$-I [”/usr/local/lib/ruby/site_ruby/1.8″, “/usr/local/lib/ruby/site_ruby/1.8/i686-darwin9.0.0″, “/usr/local/lib/ruby/site_ruby”, “/usr/local/lib/ruby/1.8″, “/usr/local/lib/ruby/1.8/i686-darwin9.0.0″, “.”] Library script path (passed via command line option -I)
$\ nil Output record separator
$= false Was last regular expression match case-sensitive?
$binding nil ?
$-v false Alias for –verbose command line option
$> # Points to standard output
$& nil Last matched regular expression (also $1, $2, etc)
$! nil Last raised exception
$PROGRAM_NAME “irb” Alias for $0
$LOADED_FEATURES [”e2mmap.rb”, “irb/init.rb”, “irb/workspace.rb”, “irb/context.rb”, “irb/extend-command.rb”, “irb/output-method.rb”, “irb/notifier.rb”, “irb/slex.rb”, “irb/ruby-token.rb”, “irb/ruby-lex.rb”, “readline.bundle”, “irb/input-method.rb”, “irb/locale.rb”, “irb.rb”] Alias for $”
$? nil Status of last executed child process (useful with Kernel#system and similar)
$; nil Input field separator
$SAFE 0 Ruby safe mode
$-d false Command line option –debug
$: [”/usr/local/lib/ruby/site_ruby/1.8″, “/usr/local/lib/ruby/site_ruby/1.8/i686-darwin9.0.0″, “/usr/local/lib/ruby/site_ruby”, “/usr/local/lib/ruby/1.8″, “/usr/local/lib/ruby/1.8/i686-darwin9.0.0″, “.”] Alias for library path(?)
$-0 “\n” Separator character(?)
$+ nil ?
$-a false Ruby command line options: auto-split mode
$VERBOSE false Ruby command line options: –verbose (some methods print extra information if this is on)
$stderr # Standard error destination
$~ nil Last matched regular expression data (instance of MatchData)
$@ nil Position of an error occurence

References

http://www.ruby-doc.org/docs/UsersGuide/rg/globalvars.html

http://cbcg.net/talks/rubyidioms/index.html

http://www.math.hokudai.ac.jp/~gotoken/ruby/ruby-uguide/uguide20.html

Hal Fulton: The Ruby Way (2nd Edition), Addison Wesley

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Reddit
  • StumbleUpon
  • Technorati
  • Facebook

3 Comments

  1. Posted May 5, 2008 at 12:12 am | Permalink

    Nice writeup.

    $` and $’ are the text before regexp match and text after regexp match, respectively.

  2. Posted May 5, 2008 at 9:04 am | Permalink

    Cheers, updated.

  3. Posted May 12, 2008 at 7:48 am | Permalink

    My version of the Pickaxe shows $FILENAME is the name of the current input file, $, is the separator string output between parameters such as print and join. $+ is for pattern matching, and returns the contents of the highest-numbered group matched following a successful pattern match (last match in a group of successful matches I guess?). Dunno about $binding though….

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*