Module: Inspectable

Overview

because overriding #to_s unfortunately wipes out #inspect

Constant Summary collapse

INSPECTING_KEY =
('Inspectable::' + '%016x' % rand(2**64)).to_sym

Instance Method Summary collapse

Instance Method Details

#inspectObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/json_patterns.rb', line 31

def inspect
  Thread.current[INSPECTING_KEY] ||= {}

  object_desc = "#{self.class}:0x#{(object_id << 1).to_s(16)}"

  if Thread.current[INSPECTING_KEY][self]
    attributes_desc = ' ...'
  else
    Thread.current[INSPECTING_KEY][self] = true

    begin
      attributes_desc = self.instance_variables.map { |v|
        " #{v.to_s}=#{instance_variable_get(v).inspect}"
      }.join
    ensure
      Thread.current[INSPECTING_KEY].delete(self)
    end
  end

  return "#<#{object_desc}#{attributes_desc}>"
end