Excellent question. Unfortunately, you just jumped down a rabbit hole, but it's one that you have to fall through eventually in ruby to start understanding the real intricacies.
For your first question, regarding the $
-prefixed global variables. They are truly global:
def mk_foo() $foo ||= "foo"; end
$foo # => nil
mk_foo # => "foo"
$foo # => "foo"
mk_foo.object_id # => 70299647799620
$foo.object_id # => 70299647799620
As you can see, when $foo
is defined within the mk_foo
method, it is defined in the global space, and you can access it anywhere:
class CanSeeFoo
def see_foo() $foo; end
end
CanSeeFoo.new.can_see_foo
# => "foo"
CanSeeFoo.new.can_see_foo.object_id
# => 70299647799620
As for the class variable question, this is where the rabbit-hole begins. First, you are correct that @@
-prefixed variables are referred to as "class variables" and @
-prefixed variables are referred to as "instance variables".
Class variables are static across all subclasses (at all sub-levels of the inheritance tree) of the defining class. The implication here is that if any subclass changes the class variable, it will change in all related subclasses and up to the defining class.
class A; end
class B < A; @@foo = "foo"; end
B.class_variable_get(:@@foo) # => "foo"
A.class_variable_get(:@@foo)
# => raises NameError "uninitialized class variable @@foo in A"
class C < B; end
C.class_variable_get(:@@foo) # => "foo"
class D < C
def self.change_foo(); @@foo = "bar"; end
def change_foo(); @@foo = "baz"; end
end
D.class_variable_get(:@@foo) # => "foo"
class E < D; end
E.class_variable_get(:@@foo) # => "foo"
D.change_foo # => "bar"
D.class_variable_get(:@@foo) # => "bar"
E.class_variable_get(:@@foo) # => "bar"
C.class_variable_get(:@@foo) # => "bar"
B.class_variable_get(:@@foo) # => "bar"
D.new.change_foo # => "baz"
D.class_variable_get(:@@foo) # => "baz"
E.class_variable_get(:@@foo) # => "baz"
C.class_variable_get(:@@foo) # => "baz"
B.class_variable_get(:@@foo) # => "baz"
A.class_variable_get(:@@foo)
# => raises NameError "uninitialized class variable @@foo in A"
As for accessing class and instance variables, neither is accessible without the use of #instance_variable_get
or ::class_variable_get
until an accessor is defined. At present, ruby only has methods for defining accessors on instance variables, but it is simple enough to define the appropriate methods for the class variables:
class A
@@foo = "foo"
# the second argument `true` adds the writer method `#bar=`
attr :bar, true
def self.foo(); @@foo; end
def self.foo=(v); @@foo = v; end
def initialize()
@bar = "bar"
end
end
class B < A; end
A.foo # => "foo"
B.foo = "foobar"
A.foo # => "foobar"
B.foo # => "foobar"
a = A.new
a.bar # => "bar"
a.bar = "baz"
a.bar # => "baz"
a.foo
# => raises NoMethodError: undefined method `foo' for #<A:0x ...
You can see the attribute accessor methods here in the ruby core docs: http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-attr. Also, ActiveSupport (http://rubygems.org/gems/activesupport) has "cattr
" methods for defining class variable accessors http://api.rubyonrails.org/v3.2.5/classes/Class.html#method-i-cattr_accessor.
That's the simple stuff. The next step is understanding the "singleton class" also known as the "eigenclass" or "metaclass" (Wikipedia: Metaclass) (remember, everything in ruby is an Object, including the Class and Module constructs). Here I will point you to an excellent post by Yehuda Katz: Metaprogramming in Ruby: It’s All About the Self, and another Stack Overflow question: class << self idiom in Ruby.
As a preview: The singleton class (not to be confused with the singleton design pattern) allows you to access methods and instance data for a specific class or module. For some related documentation, see the core docs: http://www.ruby-doc.org/core-1.9.3/Object.html#method-i-singleton_class
class A; end
class B < A;
class << self
def foo() @foo end
def foo=(v) @foo = v; end
end
end
B.foo = "foo"
class C < B; end
A.foo
# => raises NoMethodError: undefined method `foo' for A:Class
B.foo # => "foo"
C.foo # => nil
B.foo = "baz"
B.foo # => "baz"
C.foo # => nil
C.foo = "foo"
C.foo # => "foo"
B.foo # => "baz"
Lastly, remember to make use of the Ruby-Core docs. Most useful for understanding the above are: