Created
January 16, 2015 23:45
-
-
Save amanelis/7f4af80fd60ffd6c1940 to your computer and use it in GitHub Desktop.
Count the instances of an object in the code.
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 Examp | |
def self.obj_count | |
count = 0 | |
ObjectSpace.each_object(self) do |b| | |
count += 1 | |
end | |
return count | |
end | |
end | |
a = Examp.new | |
b = Examp.new | |
c = Examp.new | |
puts Examp.obj_count #=> 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment