Created
June 4, 2009 12:33
-
-
Save eee-c/123594 to your computer and use it in GitHub Desktop.
Stupid class variable "trick"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bash$ irb | |
>> class Foo; @@foo = :foo; class << self; def inside_foo; @@foo; end end end | |
=> nil | |
>> class << Foo; def outside_foo; @@foo; end; def outside_get_foo; send(:class_variable_get, :@@foo); end end | |
=> nil | |
>> Foo.inside_foo | |
=> :foo | |
>> Foo.outside_foo | |
(irb):2: warning: class variable access from toplevel singleton method | |
NameError: uninitialized class variable @@foo in Object | |
from (irb):2:in `outside_foo' | |
from (irb):4 | |
>> Foo.outside_get_foo | |
=> :foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment