diff options
-rw-r--r-- | lib/ostruct.rb | 27 | ||||
-rw-r--r-- | test/ostruct/test_ostruct.rb | 2 |
2 files changed, 13 insertions, 16 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 2edb2edeb4..6cf0ecefc3 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -1,4 +1,4 @@ -# frozen_string_literal: false +# frozen_string_literal: true # # = ostruct.rb: OpenStruct implementation # @@ -308,25 +308,20 @@ class OpenStruct # Returns a string containing a detailed summary of the keys and values. # def inspect - str = "#<#{self.class}" - 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 - str << " #{k}=#{v.inspect}" + detail = ' ...' + else + ids << object_id + begin + detail = @table.map do |key, value| + " #{key}=#{value.inspect}" + end.join(',') + ensure + ids.pop end - return str << '>' - ensure - ids.pop end + ['#<', self.class, detail, '>'].join end alias :to_s :inspect diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index 32eb73a89d..24ffb77380 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -52,12 +52,14 @@ class TC_OpenStruct < Test::Unit::TestCase foo.bar = 1 foo.baz = 2 assert_equal("#<OpenStruct bar=1, baz=2>", foo.inspect) + assert_equal(false, foo.inspect.frozen?) 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) + assert_equal(false, foo.inspect.frozen?) end def test_frozen |