Skip to content

Instantly share code, notes, and snippets.

@st0012
Last active October 4, 2016 08:33
Show Gist options
  • Save st0012/6775b5715d269bf7dc86207064d97d5a to your computer and use it in GitHub Desktop.
Save st0012/6775b5715d269bf7dc86207064d97d5a to your computer and use it in GitHub Desktop.
class variable VS metaclass's instance variable
class Foo
@@bar = "123"
def bar
@@bar
end
class << self
def set_bar(bar)
@bar = bar
end
end
end
foo = Foo.new
foo.bar
#=> "123"
Foo.set_bar("456")
#=> "456"
foo.bar
#=> "123"
Foo.instance_variable_get(:@bar)
#=> "456"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment