[前][次][番号順一覧][スレッド一覧]

ruby-changes:48292

From: marcandre <ko1@a...>
Date: Wed, 25 Oct 2017 03:08:19 +0900 (JST)
Subject: [ruby-changes:48292] marcandRe: r60406 (trunk): lib/ostruct.rb: Use frozen literals.

marcandre	2017-10-25 03:08:15 +0900 (Wed, 25 Oct 2017)

  New Revision: 60406

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60406

  Log:
    lib/ostruct.rb: Use frozen literals.
    
    Patch adapted from Espartaco Palma. [GH-1714] [Bug #14000]

  Modified files:
    trunk/lib/ostruct.rb
    trunk/test/ostruct/test_ostruct.rb
Index: test/ostruct/test_ostruct.rb
===================================================================
--- test/ostruct/test_ostruct.rb	(revision 60405)
+++ test/ostruct/test_ostruct.rb	(revision 60406)
@@ -52,12 +52,14 @@ class TC_OpenStruct < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L52
     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
Index: lib/ostruct.rb
===================================================================
--- lib/ostruct.rb	(revision 60405)
+++ lib/ostruct.rb	(revision 60406)
@@ -1,4 +1,4 @@ https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L1
-# frozen_string_literal: false
+# frozen_string_literal: true
 #
 # = ostruct.rb: OpenStruct implementation
 #
@@ -308,25 +308,20 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L308
   # 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
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]