Skip to content

Instantly share code, notes, and snippets.

@amanelis
Created January 16, 2015 23:45
Show Gist options
  • Save amanelis/7f4af80fd60ffd6c1940 to your computer and use it in GitHub Desktop.
Save amanelis/7f4af80fd60ffd6c1940 to your computer and use it in GitHub Desktop.
Count the instances of an object in the code.
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