Index: lib/ostruct.rb =================================================================== --- lib/ostruct.rb (revision 21202) +++ lib/ostruct.rb (working copy) @@ -112,25 +112,23 @@ def inspect str = "#<#{self.class}" - Thread.current[InspectKey] ||= [] - if Thread.current[InspectKey].include?(self) then - str << " ..." - else + ids = (Thread.current[InspectKey] ||= []) + if ids.include?(object_id) + return str << ' ...>' + end + + ids << object_id + begin first = true for k,v in @table str << "," unless first first = false - - Thread.current[InspectKey] << v - begin - str << " #{k}=#{v.inspect}" - ensure - Thread.current[InspectKey].pop - end + str << " #{k}=#{v.inspect}" end + return str << '>' + ensure + ids.pop end - - str << ">" end alias :to_s :inspect Index: test/ostruct/test_ostruct.rb =================================================================== --- test/ostruct/test_ostruct.rb (revision 21202) +++ test/ostruct/test_ostruct.rb (working copy) @@ -20,4 +20,12 @@ o2.instance_eval{@table = {:a => 'b'}} assert_not_equal(o1, o2) end + + def test_inspect + foo = OpenStruct.new + foo.bar = OpenStruct.new + assert_equal('#>', foo.inspect) + foo.bar.foo = foo + assert_equal('#>>', foo.inspect) + end end