diff --git a/lib/ostruct.rb b/lib/ostruct.rb index 45ebb80..b1ad116 100644 --- a/lib/ostruct.rb +++ b/lib/ostruct.rb @@ -89,7 +89,7 @@ class OpenStruct def method_missing(mid, *args) # :nodoc: mname = mid.id2name len = args.length - if mname.chomp!('=') + if mname.chomp!('=') && mname != "[]" if len != 1 raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1) end diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb index 5c140a4..7504e29 100644 --- a/test/ostruct/test_ostruct.rb +++ b/test/ostruct/test_ostruct.rb @@ -63,4 +63,15 @@ EOT assert_raise(TypeError) {o.a = 'z'} assert_equal('a', o.a) end + + def test_method_missing_handles_square_bracket_equals + o = OpenStruct.new + assert_raise(NoMethodError) { o[:foo] = :bar } + end + + def test_method_missing_handles_square_brackets + o = OpenStruct.new + assert_raise(NoMethodError) { o[:foo] } + end + end