Skip to content

Instantly share code, notes, and snippets.

@santosh79
Created November 16, 2009 03:27
Show Gist options
  • Save santosh79/235704 to your computer and use it in GitHub Desktop.
Save santosh79/235704 to your computer and use it in GitHub Desktop.
#Keeping track of number instances of a class without actually creating getter and setter methods
class Foo
@count = 0
#Create a singleton class for self - which is currently the Foo class object
class << self
attr_accessor :count
end
def initialize
Foo.count += 1 #Increment the counter when a new instance is created
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment