Module: ObjectSpaceDiff

Defined in:
lib/object_space_diff.rb,
lib/object_space_diff/version.rb

Overview

$: << ‘./lib’ ; require ‘object_space_diff’; ObjectSpaceDiff.puts_changes{}

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.puts_changes(target = $stdout) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/object_space_diff.rb', line 5

def puts_changes(target=$stdout)
  klass = Object
  object_ids = ObjectSpace.each_object(klass).collect(&:object_id)
  data = yield


  ObjectSpace.garbage_collect
  sleep 1
  ObjectSpace.garbage_collect

  new_ids = ObjectSpace.each_object(klass).collect(&:object_id)
  ids_diff = (new_ids - object_ids)

  (ids_diff).each_with_index do |oid,i|
    if oid == object_ids.object_id ||
        oid == new_ids.object_id ||
        oid == ids_diff.object_id ||
        oid == data.object_id ||
        oid == klass.object_id
      target.puts "#{i+1} #{oid}: junk_collector_internal, skipping" if $DEBUG
      next
    end
    obj = ObjectSpace._id2ref(oid)
    if obj.kind_of?(Array) && obj.first.object_id == klass.object_id
      target.puts "#{i+1} #{oid}: junk_collector_internal, skipping" if $DEBUG
    else
      target.puts "#{i+1} #{oid}:\n\t\t#{obj.class}\n\t\t#{obj}"
    end
    obj = nil
  end
  new_ids = nil
  object_ids = nil
  klass = nil
  data
end