ruby-changes:62951
From: Marc-Andre <ko1@a...>
Date: Tue, 15 Sep 2020 01:47:35 +0900 (JST)
Subject: [ruby-changes:62951] 8eefa8f373 (master): [ruby/ostruct] Allow overriding public methods
https://git.ruby-lang.org/ruby.git/commit/?id=8eefa8f373 From 8eefa8f3736cd5dbf7256f571b368198102f11cc Mon Sep 17 00:00:00 2001 From: Marc-Andre Lafortune <github@m...> Date: Tue, 8 Sep 2020 16:30:02 -0400 Subject: [ruby/ostruct] Allow overriding public methods [Fixes https://bugs.ruby-lang.org/issues/15409] diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 18850bf..5f3d301 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -100,9 +100,9 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L100 # Duplicates an OpenStruct object's Hash table. def initialize_copy(orig) # :nodoc: + orig.table.each_key{|key| new_ostruct_member!(key)} super @table = @table.dup - @table.each_key{|key| new_ostruct_member!(key)} end # @@ -159,8 +159,8 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L159 # Provides marshalling support for use by the Marshal library. # def marshal_load(x) + x.each_key{|key| new_ostruct_member!(key)} @table = x - @table.each_key{|key| new_ostruct_member!(key)} end # @@ -169,7 +169,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L169 # define_singleton_method for both the getter method and the setter method. # def new_ostruct_member!(name) # :nodoc: - unless respond_to?(name) + unless @table.key?(name) define_singleton_method(name) { @table[name] } define_singleton_method("#{name}=") {|x| @table[name] = x} end diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index 0688d89..3c934cc 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -235,4 +235,10 @@ class TC_OpenStruct < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ostruct/test_ostruct.rb#L235 assert_equal(:foo, os.puts) assert_equal(:bar, os.format) end + + def test_overriden_public_methods + os = OpenStruct.new(method: :foo, class: :bar) + assert_equal(:foo, os.method) + assert_equal(:bar, os.class) + end end -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/