Skip to content

Instantly share code, notes, and snippets.

@mark
Created October 15, 2013 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mark/6997312 to your computer and use it in GitHub Desktop.
Save mark/6997312 to your computer and use it in GitHub Desktop.
A method for tracking the change in counts of objects in ObjectSpace
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