View Global and Local Variables
suggest changeThe Kernel exposes methods for getting the list of global_variables and local_variables. This list is from Ruby 4, before any regexp match, so $1 through $9 are absent:
cats = 42
$demo = "in progress"
p global_variables.sort
#=> [:$!, :$", :$$, :$&, :$', :$*, :$+, :$,, :$-0, :$-F, :$-I, :$-W, :$-a, :$-d,
#=> :$-i, :$-l, :$-p, :$-v, :$-w, :$., :$/, :$0, :$:, :$;, :$<, :$=, :$>, :$?,
#=> :$@, :$DEBUG, :$FILENAME, :$LOADED_FEATURES, :$LOAD_PATH, :$PROGRAM_NAME,
#=> :$VERBOSE, :$\, :$_, :$`, :$demo, :$stderr, :$stdin, :$stdout, :$~]
p local_variables
#=> [:cats]
Unlike instance variables, there is no method that removes a global or local variable. Locals can be read and set with Binding#local_variable_get and Binding#local_variable_set. Looking for this kind of access is usually a sign that your code should be rewritten to use a Hash to store the values. If you must modify global or local variables by name, you can use eval with a string:
var = "$demo"
eval(var) #=> "in progress"
eval("#{var} = 17")
p $demo #=> 17
By default, eval will evaluate your variables in the current scope. To evaluate local variables in a different scope, you must capture the binding where the local variables exist.
def local_variable_get(name, bound=nil)
foo = :inside
eval(name,bound)
end
def test_1
foo = :outside
p local_variable_get("foo")
end
def test_2
foo = :outside
p local_variable_get("foo",binding)
end
test_1 #=> :inside
test_2 #=> :outside
In the above, test_1 did not pass a binding to local_variable_get, and so the eval was executed within the context of that method, where a local variable named foo was set to :inside.