diff options
author | Marc-Andre Lafortune <[email protected]> | 2020-09-14 13:48:29 -0400 |
---|---|---|
committer | Marc-André Lafortune <[email protected]> | 2020-09-14 16:10:37 -0400 |
commit | 606c009ce24bd8e9e07ecb8f920a77c005062ff5 (patch) | |
tree | ce09e8c5a70da914079172c4b89f00577bf9be8d | |
parent | 67e5f7a9e508d6f33c1dd927753161e8b1d40a09 (diff) |
[ruby/ostruct] Avoid self calling our public methods.
Found because `json` has a bad example in its test suite.
This implementation still offers better encapsulation.
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/3541
-rw-r--r-- | lib/ostruct.rb | 6 | ||||
-rw-r--r-- | test/ostruct/test_ostruct.rb | 18 |
2 files changed, 22 insertions, 2 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb index d674274966..9b44d4a88a 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -124,7 +124,7 @@ class OpenStruct @table = {} if hash hash.each_pair do |k, v| - self[k] = v + set_ostruct_member_value!(k, v) end end end @@ -218,7 +218,7 @@ class OpenStruct if len != 1 raise ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1) end - self[mname]= args[0] + set_ostruct_member_value!(mname, args[0]) elsif len == 0 elsif @table.key?(mid) raise ArgumentError, "wrong number of arguments (given #{len}, expected 0)" @@ -262,6 +262,8 @@ class OpenStruct new_ostruct_member!(name) @table[name] = value end + alias_method :set_ostruct_member_value!, :[]= + private :set_ostruct_member_value! # :call-seq: # ostruct.dig(name, *identifiers) -> object diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index d07fef3a83..560979e887 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -246,4 +246,22 @@ class TC_OpenStruct < Test::Unit::TestCase os = OpenStruct.new(method: :foo) assert_equal(os.object_id, os.method!(:object_id).call) end + + def test_mistaken_subclass + sub = Class.new(OpenStruct) do + def [](k) + __send__(k) + super + end + + def []=(k, v) + @item_set = true + __send__("#{k}=", v) + super + end + end + o = sub.new + o.foo = 42 + assert_equal 42, o.foo + end end |