Skip to content

Instantly share code, notes, and snippets.

@eee-c
Created June 4, 2009 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eee-c/123594 to your computer and use it in GitHub Desktop.
Save eee-c/123594 to your computer and use it in GitHub Desktop.
Stupid class variable "trick"
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