Skip to content

Instantly share code, notes, and snippets.

@woods
Created April 22, 2009 20:14
Show Gist options
  • Save woods/100045 to your computer and use it in GitHub Desktop.
Save woods/100045 to your computer and use it in GitHub Desktop.
class Thing
# The number of instances of this class that are currently in existence.
@@count = 0
# Print out how many thing there are.
def self.print_count
puts "There are now #{@@count} things."
end
def initialize
# Tally one more thing.
@@count += 1
Thing.print_count
# When this object is destroyed, tally one fewer thing.
ObjectSpace.define_finalizer(self) do
@@count -= 1
Thing.print_count
end
end
end
puts "Creating things."
10.times { Thing.new }
puts "Exiting."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment