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

ruby-changes:62948

From: Marc-Andre <ko1@a...>
Date: Tue, 15 Sep 2020 01:47:31 +0900 (JST)
Subject: [ruby-changes:62948] e026e186f4 (master): [ruby/ostruct] Revert "ostruct.rb: deferred accessors"

https://git.ruby-lang.org/ruby.git/commit/?id=e026e186f4

From e026e186f4a01aa3f6cd02ae6ef33f44f129361c Mon Sep 17 00:00:00 2001
From: Marc-Andre Lafortune <github@m...>
Date: Tue, 8 Sep 2020 15:08:50 -0400
Subject: [ruby/ostruct] Revert "ostruct.rb: deferred accessors"

This reverts commits:
dc38e99813
22c082fcfd
b499e0f9ff
58e5876646

Add test for overriden private methods

[Fixes https://bugs.ruby-lang.org/issues/12136]

diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index b49df2d..477b67c 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -95,6 +95,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L95
       hash.each_pair do |k, v|
         k = k.to_sym
         @table[k] = v
+        new_ostruct_member!(k)
       end
     end
   end
@@ -103,6 +104,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L104
   def initialize_copy(orig) # :nodoc:
     super
     @table = @table.dup
+    @table.each_key{|key| new_ostruct_member!(key)}
   end
 
   #
@@ -160,6 +162,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L162
   #
   def marshal_load(x)
     @table = x
+    @table.each_key{|key| new_ostruct_member!(key)}
   end
 
   #
@@ -183,7 +186,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L186
   #
   def new_ostruct_member!(name) # :nodoc:
     name = name.to_sym
-    unless singleton_class.method_defined?(name)
+    unless respond_to?(name)
       define_singleton_method(name) { @table[name] }
       define_singleton_method("#{name}=") {|x| modifiable?[name] = x}
     end
@@ -191,16 +194,6 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L194
   end
   private :new_ostruct_member!
 
-  def freeze
-    @table.each_key {|key| new_ostruct_member!(key)}
-    super
-  end
-
-  def respond_to_missing?(mid, include_private = false) # :nodoc:
-    mname = mid.to_s.chomp("=").to_sym
-    defined?(@table) && @table.key?(mname) || super
-  end
-
   def method_missing(mid, *args) # :nodoc:
     len = args.length
     if mname = mid[/.*(?==\z)/m]
@@ -208,11 +201,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L201
         raise ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
       end
       modifiable?[new_ostruct_member!(mname)] = args[0]
-    elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid #
-      if @table.key?(mid)
-        new_ostruct_member!(mid) unless frozen?
-        @table[mid]
-      end
+    elsif len == 0
     elsif @table.key?(mid)
       raise ArgumentError, "wrong number of arguments (given #{len}, expected 0)"
     else
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index 8315980..3917cc0 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -179,7 +179,6 @@ class TC_OpenStruct < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L179
   def test_accessor_defines_method
     os = OpenStruct.new(foo: 42)
     assert_respond_to(os, :foo)
-    assert_equal([], os.singleton_methods)
     assert_equal(42, os.foo)
     assert_equal([:foo, :foo=], os.singleton_methods.sort)
   end
@@ -225,4 +224,10 @@ class TC_OpenStruct < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L224
       os.foo true, true
     end
   end
+
+  def test_overriden_private_methods
+    os = OpenStruct.new(puts: :foo, format: :bar)
+    assert_equal(:foo, os.puts)
+    assert_equal(:bar, os.format)
+  end
 end
-- 
cgit v0.10.2


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

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