summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--lib/ostruct.rb6
-rw-r--r--test/ostruct/test_ostruct.rb11
3 files changed, 19 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index ca1eb95..46e2aa5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sat May 28 00:58:40 2011 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * 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@cozmixng.org>
* test/rexml/test_core.rb (Tester::test_text_frozen): split frozen
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 33d669e..f3b4608 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -177,15 +177,15 @@ class OpenStruct
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
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index 5c83bfd..db8e741 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -61,4 +61,15 @@ class TC_OpenStruct < Test::Unit::TestCase
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