summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/ostruct.rb6
-rw-r--r--test/ostruct/test_ostruct.rb18
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