ruby-changes:19708
From: marcandre <ko1@a...>
Date: Sat, 28 May 2011 00:59:09 +0900 (JST)
Subject: [ruby-changes:19708] marcandRe: r31753 (trunk): * lib/ostruct.rb (method_missing): Handle [] and []= correctly.
marcandre 2011-05-28 00:59:02 +0900 (Sat, 28 May 2011) New Revision: 31753 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=31753 Log: * lib/ostruct.rb (method_missing): Handle [] and []= correctly. Based on a patch by Caius Durling, bug #4179 [ruby-core:33792] Modified files: trunk/ChangeLog trunk/lib/ostruct.rb trunk/test/ostruct/test_ostruct.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 31752) +++ ChangeLog (revision 31753) @@ -1,3 +1,8 @@ +Sat May 28 00:58:40 2011 Marc-Andre Lafortune <ruby-core@m...> + + * lib/ostruct.rb (method_missing): Handle [] and []= correctly. + Based on a patch by Caius Durling, bug #4179 [ruby-core:33792] + Fri May 27 23:56:54 2011 Kouhei Sutou <kou@c...> * test/rexml/test_core.rb (Tester::test_text_frozen): split frozen Index: lib/ostruct.rb =================================================================== --- lib/ostruct.rb (revision 31752) +++ lib/ostruct.rb (revision 31753) @@ -177,15 +177,15 @@ def method_missing(mid, *args) # :nodoc: mname = mid.id2name len = args.length - if mname.chomp!('=') + if mname.chomp!('=') && mid != :[]= if len != 1 raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1) end modifiable[new_ostruct_member(mname)] = args[0] - elsif len == 0 + elsif len == 0 && mid != :[] @table[mid] else - raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1) + raise NoMethodError, "undefined method `#{mid}' for #{self}", caller(1) end end Index: test/ostruct/test_ostruct.rb =================================================================== --- test/ostruct/test_ostruct.rb (revision 31752) +++ test/ostruct/test_ostruct.rb (revision 31753) @@ -61,4 +61,15 @@ assert_not_respond_to(o, :a, bug) assert_not_respond_to(o, :a=, bug) 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/