Skip to content

Instantly share code, notes, and snippets.

@detunized
Created January 16, 2012 12:24
Show Gist options
  • Save detunized/1620634 to your computer and use it in GitHub Desktop.
Save detunized/1620634 to your computer and use it in GitHub Desktop.
List global Ruby variables and their values
global_variables.sort.each do |name|
puts "#{name}: #{eval "#{name}.inspect"}"
end
$!: nil
$": ["enumerator.so"]
$$: 9472
$&: nil
$': nil
$*: []
$+: nil
$,: nil
$-0: "\n"
$-F: nil
$-I: ["C:/ruby/lib/ruby/site_ruby/1.8", "C:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt", "C:/ruby/lib/ruby/site_ruby", "C:/ruby/lib/ruby/vendor_ruby/1.8", "C:/ruby/lib/ruby/vendor_ruby/1.8/i386-msvcrt", "C:/ruby/lib/ruby/vendor_ruby", "C:/ruby/lib/ruby/1.8", "C:/ruby/lib/ruby/1.8/i386-mingw32", "."]
$-K: "NONE"
$-a: false
$-d: false
$-i: nil
$-l: false
$-p: false
$-v: false
$-w: false
$.: 3
$/: "\n"
$0: "globals.rb"
$:: ["C:/ruby/lib/ruby/site_ruby/1.8", "C:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt", "C:/ruby/lib/ruby/site_ruby", "C:/ruby/lib/ruby/vendor_ruby/1.8", "C:/ruby/lib/ruby/vendor_ruby/1.8/i386-msvcrt", "C:/ruby/lib/ruby/vendor_ruby", "C:/ruby/lib/ruby/1.8", "C:/ruby/lib/ruby/1.8/i386-mingw32", "."]
$;: nil
$<: ARGF
$=: false
$>: #<IO:0x3f2dab0>
$?: nil
$@: nil
$DEBUG: false
$FILENAME: "-"
$KCODE: "NONE"
$LOADED_FEATURES: ["enumerator.so"]
$LOAD_PATH: ["C:/ruby/lib/ruby/site_ruby/1.8", "C:/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt", "C:/ruby/lib/ruby/site_ruby", "C:/ruby/lib/ruby/vendor_ruby/1.8", "C:/ruby/lib/ruby/vendor_ruby/1.8/i386-msvcrt", "C:/ruby/lib/ruby/vendor_ruby", "C:/ruby/lib/ruby/1.8", "C:/ruby/lib/ruby/1.8/i386-mingw32", "."]
$PROGRAM_NAME: "globals.rb"
$SAFE: 0
$VERBOSE: false
$\: nil
$_: nil
$`: nil
$deferr: #<IO:0x3f2da98>
$defout: #<IO:0x3f2dab0>
$stderr: #<IO:0x3f2da98>
$stdin: #<IO:0x3f2dac8>
$stdout: #<IO:0x3f2dab0>
$~: nil
@matthewhively
Copy link

As a note when I ran this in ruby 2.7.8 I saw printouts of:
(eval):1: warning: variable $KCODE is no longer effective
and
(eval):1: warning: variable $= is no longer effective
and
(eval):1: warning: $SAFE will become a normal global variable in Ruby 3.0

Additionally I ran into some problems with the eval causing exceptions sometimes, so I added a rescue block to the loop to skip over those.
So effectively this:

global_variables.sort.each do |name|
  puts "#{name}: #{eval "#{name}.inspect"}"
rescue Errno::ENOENT
  puts "caught error for '#{name}'"
end

@detunized
Copy link
Author

Wow! 13 years old. I guess a lot changed in the Ruby land since then =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment