Skip to content

Instantly share code, notes, and snippets.

@ifyouseewendy
Last active August 29, 2015 14:07
Show Gist options
  • Save ifyouseewendy/ad45ba795001580f4853 to your computer and use it in GitHub Desktop.
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*.
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