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)
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
test/ostruct/test_ostruct.rb (working copy)
o2.instance_eval{@table = {:a => 'b'}}
assert_not_equal(o1, o2)
end
def test_inspect
foo = OpenStruct.new
foo.bar = OpenStruct.new
assert_equal('#<OpenStruct bar=#<OpenStruct>>', foo.inspect)
foo.bar.foo = foo
assert_equal('#<OpenStruct bar=#<OpenStruct foo=#<OpenStruct ...>>>', foo.inspect)
end
end
    (1-1/1)