diff options
author | Marc-Andre Lafortune <github@marc-andre.ca> | 2020-09-08 15:08:50 -0400 |
---|---|---|
committer | Marc-Andre Lafortune <github@marc-andre.ca> | 2020-09-14 12:46:27 -0400 |
commit | e026e186f4a01aa3f6cd02ae6ef33f44f129361c (patch) | |
tree | 4ed6005deb54e1b1f4dae538d177577a6c28f431 | |
parent | 1cabb216c6d44ca948a65544dba73ad75bbc8ec4 (diff) | |
download | ruby-e026e186f4a01aa3f6cd02ae6ef33f44f129361c.tar.gz |
[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]
-rw-r--r-- | lib/ostruct.rb | 21 | ||||
-rw-r--r-- | test/ostruct/test_ostruct.rb | 7 |
2 files changed, 11 insertions, 17 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb index b49df2d2d6..477b67c0d5 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -95,6 +95,7 @@ class OpenStruct 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 def initialize_copy(orig) # :nodoc: super @table = @table.dup + @table.each_key{|key| new_ostruct_member!(key)} end # @@ -160,6 +162,7 @@ class OpenStruct # def marshal_load(x) @table = x + @table.each_key{|key| new_ostruct_member!(key)} end # @@ -183,7 +186,7 @@ class OpenStruct # 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 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 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 831598086d..3917cc0417 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -179,7 +179,6 @@ class TC_OpenStruct < Test::Unit::TestCase 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 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 |