Skip to content

Instantly share code, notes, and snippets.

@JohnMorales
Created September 29, 2013 14:44
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 JohnMorales/6753096 to your computer and use it in GitHub Desktop.
Save JohnMorales/6753096 to your computer and use it in GitHub Desktop.
class MyClass
@@number_of_objects = 0
def self.how_many_objects?
@@number_of_objects
end
def initialize
@@number_of_objects = @@number_of_objects.next # 'number_of_objects' asscesible to object instances of the class. (i.e. initialize is an instance method..)
end
def foo
end
end
class BetterMyClass < MyClass
def bar
end
end
a = MyClass.new
b = BetterMyClass.new
p MyClass.how_many_objects? #=> 2
p BetterMyClass.how_many_objects? #=> 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment