Last active
October 4, 2016 08:33
-
-
Save st0012/6775b5715d269bf7dc86207064d97d5a to your computer and use it in GitHub Desktop.
class variable VS metaclass's instance variable
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
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