Project

General

Profile

Backport #954 ยป fix-ostruct-inspect.diff

Patch for OpenStruct#inspect with test case - murphy (Kornelius Kalnbach), 12/30/2008 11:49 PM

View differences:

lib/ostruct.rb (working copy)
112 112
  def inspect
113 113
    str = "#<#{self.class}"
114 114

  
115
    Thread.current[InspectKey] ||= []
116
    if Thread.current[InspectKey].include?(self) then
117
      str << " ..."
118
    else
115
    ids = (Thread.current[InspectKey] ||= [])
116
    if ids.include?(object_id)
117
      return str << ' ...>'
118
    end
119

  
120
    ids << object_id
121
    begin
119 122
      first = true
120 123
      for k,v in @table
121 124
        str << "," unless first
122 125
        first = false
123

  
124
        Thread.current[InspectKey] << v
125
        begin
126
          str << " #{k}=#{v.inspect}"
127
        ensure
128
          Thread.current[InspectKey].pop
129
        end
126
        str << " #{k}=#{v.inspect}"
130 127
      end
128
      return str << '>'
129
    ensure
130
      ids.pop
131 131
    end
132

  
133
    str << ">"
134 132
  end
135 133
  alias :to_s :inspect
136 134

  
test/ostruct/test_ostruct.rb (working copy)
20 20
    o2.instance_eval{@table = {:a => 'b'}}
21 21
    assert_not_equal(o1, o2)
22 22
  end
23
  
24
  def test_inspect
25
    foo = OpenStruct.new
26
    foo.bar = OpenStruct.new
27
    assert_equal('#<OpenStruct bar=#<OpenStruct>>', foo.inspect)
28
    foo.bar.foo = foo
29
    assert_equal('#<OpenStruct bar=#<OpenStruct foo=#<OpenStruct ...>>>', foo.inspect)
30
  end
23 31
end