Kernel
, Object
, and Module
define reflective methods for
listing the names (as strings) of all defined global variables,
currently defined local variables, all instance variables of an object,
all class variables of a class or module, and all constants of a class
or module:
global_variables # => ["$DEBUG", "$SAFE", ...] x = 1 # Define a local variable local_variables # => ["x"] # Define a simple class class Point def initialize(x,y); @x,@y = x,y; end # Define instance variables @@classvar = 1 # Define a class variable ORIGIN = Point.new(0,0) # Define a constant end Point::ORIGIN.instance_variables # => ["@y", "@x"] Point.class_variables # => ["@@classvar"] Point.constants # => ["ORIGIN"]
The global_variables
, local_variables
, instance_variables
, class_variables
, and constants
methods return arrays of strings in
Ruby 1.8 and arrays of symbols in Ruby 1.9.
In addition to listing defined variables and constants, Ruby
Object
and Module
also define reflective methods for
querying, setting, and removing instance variables, class variables, and
constants. There are no special purpose methods for querying or
setting local variables or global variables, but you can use the
eval
method for this
purpose:
x = 1 varname = "x" eval(varname) # => 1 eval("varname = '$g'") # Set varname to "$g" eval("#{varname} = x") # Set $g to 1 eval(varname) # => 1
Note that eval
evaluates its
code in a temporary scope. eval
can
alter the value of local variables that already exist. But any new
local variables defined by the evaluated code are local to the
invocation of eval
and cease to
exist when it returns. (It is as if the evaluated code is run in the
body of a block—variables local to a block do not exist outside the
block.)
You can query, set, and test the existence of instance variables on any object and of class variables and constants on any class or module:
o = Object.new o.instance_variable_set(:@x, 0) # Note required @ prefix o.instance_variable_get(:@x) # => 0 o.instance_variable_defined?(:@x) # => true Object.class_variable_set(:@@x, 1) # Private in Ruby 1.8 Object.class_variable_get(:@@x) # Private in Ruby 1.8 Object.class_variable_defined?(:@@x) # => true; Ruby 1.9 and later Math.const_set(:EPI, Math::E*Math::PI) Math.const_get(:EPI) # => 8.53973422267357 Math.const_defined? :EPI # => true
In Ruby 1.9, you can pass false
as the second argument to const_get
and
const_defined?
to specify that
these methods should only look at the current class or module and
should not consider inherited constants.
The methods for querying and setting class variables are private
in Ruby 1.8. In that version, you can invoke them with class_eval
:
String.class_eval { class_variable_set(:@@x, 1) } # Set @@x in String String.class_eval { class_variable_get(:@@x) } # => 1
Object
and Module
define private methods for undefining
instance variables, class variables, and constants. They all return
the value of the removed variable or constant. Because these methods
are private, you can’t invoke them directly on an object, class, or
module, and you must use an eval
method or the send
method
(described later in this chapter):
o.instance_eval { remove_instance_variable :@x } String.class_eval { remove_class_variable(:@@x) } Math.send :remove_const, :EPI # Use send to invoke private method
The const_missing
method of a
module is invoked, if there is one, when a reference is made to an
undefined constant. You can define this method to return the value of
the named constant. (This feature can be used, for example, to
implement an autoload facility in which classes or modules are loaded
on demand.) Here is a simpler example:
def Symbol.const_missing(name) name # Return the constant name as a symbol end Symbol::Test # => :Test: undefined constant evaluates to a Symbol
18.118.152.58