Created
April 22, 2009 20:14
-
-
Save woods/100045 to your computer and use it in GitHub Desktop.
This file contains 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 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