Last active
August 29, 2015 14:07
-
-
Save ifyouseewendy/ad45ba795001580f4853 to your computer and use it in GitHub Desktop.
an example comparing *instance variable*, *class instance variable*, and *singleton class instance variable*.
This file contains hidden or 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 | |
def i_method | |
@foo = '' | |
end | |
@bar = '' | |
class << self | |
@baz = '' | |
def c_method | |
@bar2 = '' | |
end | |
end | |
end | |
Foo.singleton_class.instance_variables | |
# => [:@baz] | |
Foo.instance_variables | |
# => [:@bar] | |
Foo.c_method | |
Foo.instance_variables | |
# => => [:@bar, :@bar2] | |
a = Foo.new | |
a.instance_variables | |
# => [] | |
a.i_method | |
a.instance_variables | |
# => [:@foo] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment