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

ruby-changes:46006

From: nobu <ko1@a...>
Date: Fri, 24 Mar 2017 21:17:00 +0900 (JST)
Subject: [ruby-changes:46006] nobu:r58077 (trunk): ostruct.rb: fix OpenStruct.allocate

nobu	2017-03-24 21:16:54 +0900 (Fri, 24 Mar 2017)

  New Revision: 58077

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

  Log:
    ostruct.rb: fix OpenStruct.allocate
    
    * lib/ostruct.rb (OpenStruct.allocate): initialize an instance
      variable directly, without calling `intialize` method which may
      be overridden in a subclass.  [ruby-core:80292] [Bug #13358]

  Modified files:
    trunk/lib/ostruct.rb
    trunk/test/ostruct/test_ostruct.rb
Index: test/ostruct/test_ostruct.rb
===================================================================
--- test/ostruct/test_ostruct.rb	(revision 58076)
+++ test/ostruct/test_ostruct.rb	(revision 58077)
@@ -183,4 +183,13 @@ class TC_OpenStruct < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L183
     os.foo = 44
     assert_equal(43, os.foo)
   end
+
+  def test_allocate_subclass
+    bug = '[ruby-core:80292] [Bug #13358] allocate should not call initialize'
+    c = Class.new(OpenStruct) {
+      def initialize(x,y={})super(y);end
+    }
+    os = assert_nothing_raised(ArgumentError, bug) {c.allocate}
+    assert_instance_of(c, os)
+  end
 end
Index: lib/ostruct.rb
===================================================================
--- lib/ostruct.rb	(revision 58076)
+++ lib/ostruct.rb	(revision 58077)
@@ -74,7 +74,10 @@ https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L74
 #
 class OpenStruct
   class << self # :nodoc:
-    alias allocate new
+    def allocate
+      (x = super).instance_variable_set(:@table, {})
+      x
+    end
   end
 
   #

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

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