Created
May 26, 2015 19:28
-
-
Save pjones/fddae91247a37cda521c to your computer and use it in GitHub Desktop.
Counting classes created with `Class.new`
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
#!/usr/bin/env ruby | |
# Don't use this in production! | |
module ClassCounter | |
def self.extended (klass) | |
$stderr.puts("Injecting hacked `new' into `#{klass}'") | |
klass.instance_exec {@klass_count = 0} | |
end | |
def new (*) | |
super.tap do |klass| | |
@klass_count += 1 | |
$stderr.puts("New class: #{klass} count: #@klass_count") | |
end | |
end | |
end | |
# Override Class::new. | |
Class.extend(ClassCounter) | |
# Test it out. | |
something = Class.new | |
another = Class.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome! 😄
one more question 😀
Calling
Name.new
will outputCreating a new Name
since we modify the instance methodnew
of Class, but why the "class" methodnew
also got effected and outputCreating a new Class
when I callClass.new
?