lib/ostruct.rb: Use frozen literals.

Patch adapted from Espartaco Palma. [GH-1714] [Bug #14000]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60406 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2017-10-24 18:08:15 +00:00
parent e077e7956a
commit e565c838ce
2 changed files with 13 additions and 16 deletions

View File

@ -1,4 +1,4 @@
# frozen_string_literal: false # frozen_string_literal: true
# #
# = ostruct.rb: OpenStruct implementation # = ostruct.rb: OpenStruct implementation
# #
@ -308,26 +308,21 @@ class OpenStruct
# Returns a string containing a detailed summary of the keys and values. # Returns a string containing a detailed summary of the keys and values.
# #
def inspect def inspect
str = "#<#{self.class}"
ids = (Thread.current[InspectKey] ||= []) ids = (Thread.current[InspectKey] ||= [])
if ids.include?(object_id) if ids.include?(object_id)
return str << ' ...>' detail = ' ...'
end else
ids << object_id ids << object_id
begin begin
first = true detail = @table.map do |key, value|
for k,v in @table " #{key}=#{value.inspect}"
str << "," unless first end.join(',')
first = false
str << " #{k}=#{v.inspect}"
end
return str << '>'
ensure ensure
ids.pop ids.pop
end end
end end
['#<', self.class, detail, '>'].join
end
alias :to_s :inspect alias :to_s :inspect
attr_reader :table # :nodoc: attr_reader :table # :nodoc:

View File

@ -52,12 +52,14 @@ class TC_OpenStruct < Test::Unit::TestCase
foo.bar = 1 foo.bar = 1
foo.baz = 2 foo.baz = 2
assert_equal("#<OpenStruct bar=1, baz=2>", foo.inspect) assert_equal("#<OpenStruct bar=1, baz=2>", foo.inspect)
assert_equal(false, foo.inspect.frozen?)
foo = OpenStruct.new foo = OpenStruct.new
foo.bar = OpenStruct.new foo.bar = OpenStruct.new
assert_equal('#<OpenStruct bar=#<OpenStruct>>', foo.inspect) assert_equal('#<OpenStruct bar=#<OpenStruct>>', foo.inspect)
foo.bar.foo = foo foo.bar.foo = foo
assert_equal('#<OpenStruct bar=#<OpenStruct foo=#<OpenStruct ...>>>', foo.inspect) assert_equal('#<OpenStruct bar=#<OpenStruct foo=#<OpenStruct ...>>>', foo.inspect)
assert_equal(false, foo.inspect.frozen?)
end end
def test_frozen def test_frozen