summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-28 09:21:58 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-03-28 09:21:58 +0000
commita20715c8ed16616ab00674f134654f8be88e2033 (patch)
tree7c7f6a770c6bc0e932aa2b0cdd3490501ae20856
parent50d6291d864a50a9653d65e8c8b1ab4b310b3e54 (diff)
ostruct.rb: refine visibility failure message
* lib/ostruct.rb (method_missing): raise an exception with proper visibility message. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58195 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--lib/ostruct.rb11
-rw-r--r--test/ostruct/test_ostruct.rb24
2 files changed, 31 insertions, 4 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index 791471391c..25ccac8dfa 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -208,15 +208,18 @@ class OpenStruct
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 # and /\A[a-z_]\w*\z/ =~ mid #
if @table.key?(mid)
new_ostruct_member!(mid) unless frozen?
@table[mid]
end
else
- err = NoMethodError.new "undefined method `#{mid}' for #{self}", mid, args
- err.set_backtrace caller(1)
- raise err
+ begin
+ super
+ rescue NoMethodError => err
+ err.backtrace.shift
+ raise
+ end
end
end
diff --git a/test/ostruct/test_ostruct.rb b/test/ostruct/test_ostruct.rb
index b9bbaace55..32eb73a89d 100644
--- a/test/ostruct/test_ostruct.rb
+++ b/test/ostruct/test_ostruct.rb
@@ -192,4 +192,28 @@ class TC_OpenStruct < Test::Unit::TestCase
os = assert_nothing_raised(ArgumentError, bug) {c.allocate}
assert_instance_of(c, os)
end
+
+ def test_private_method
+ os = OpenStruct.new
+ class << os
+ private
+ def foo
+ end
+ end
+ assert_raise_with_message(NoMethodError, /private method/) do
+ os.foo true, true
+ end
+ end
+
+ def test_protected_method
+ os = OpenStruct.new
+ class << os
+ protected
+ def foo
+ end
+ end
+ assert_raise_with_message(NoMethodError, /protected method/) do
+ os.foo true, true
+ end
+ end
end