Created
October 15, 2013 19:29
-
-
Save mark/6997312 to your computer and use it in GitHub Desktop.
A method for tracking the change in counts of objects in ObjectSpace
This file contains hidden or 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
module Kernel | |
def object_count_change(root = ActiveRecord::Base) | |
before = Hash.new(0).tap { |hash| ObjectSpace.each_object(root) { |obj| hash[obj.class.to_s] += 1 } } | |
yield | |
after = Hash.new(0).tap { |hash| ObjectSpace.each_object(root) { |obj| hash[obj.class.to_s] += 1 } } | |
Hash.new.tap do |change| | |
all_keys = (before.keys + after.keys).uniq.sort | |
all_keys.each { |key| change[key] = after[key] - before[key] } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment