aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-04-14 13:47:24 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-04-14 13:47:24 +0000
commit816c5323fe82a7a4502f35ab8252ed56a6251688 (patch)
treef8c1d1bc456417a67004edf8d52a1e8483a9baf3
parent1119bb4794b32fc0edfc95ba12f02bf151c170c5 (diff)
downloadruby-816c5323fe82a7a4502f35ab8252ed56a6251688.tar.gz
OpenStruct: improve error message when passing wrong number of arguments.
Patch by Lisa Ugray (issue #15515) git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67556 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--lib/ostruct.rb4
-rw-r--r--spec/ruby/library/openstruct/method_missing_spec.rb9
2 files changed, 11 insertions, 2 deletions
diff --git a/lib/ostruct.rb b/lib/ostruct.rb
index a758a65979..c3b0546d5f 100644
--- a/lib/ostruct.rb
+++ b/lib/ostruct.rb
@@ -213,7 +213,7 @@ class OpenStruct
len = args.length
if mname = mid[/.*(?==\z)/m]
if len != 1
- raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
+ raise ArgumentError, "wrong number of arguments (given #{len}, expected 1)", caller(1)
end
modifiable?[new_ostruct_member!(mname)] = args[0]
elsif len == 0 # and /\A[a-z_]\w*\z/ =~ mid #
@@ -221,6 +221,8 @@ class OpenStruct
new_ostruct_member!(mid) unless frozen?
@table[mid]
end
+ elsif @table.key?(mid)
+ raise ArgumentError, "wrong number of arguments (given #{len}, expected 0)"
else
begin
super
diff --git a/spec/ruby/library/openstruct/method_missing_spec.rb b/spec/ruby/library/openstruct/method_missing_spec.rb
index eefe30661a..fe955791af 100644
--- a/spec/ruby/library/openstruct/method_missing_spec.rb
+++ b/spec/ruby/library/openstruct/method_missing_spec.rb
@@ -32,10 +32,17 @@ describe "OpenStruct#method_missing when called with a method name ending in '='
end
describe "OpenStruct#method_missing when passed additional arguments" do
- it "raises a NoMethodError" do
+ it "raises a NoMethodError when the key does not exist" do
os = OpenStruct.new
lambda { os.method_missing(:test, 1, 2, 3) }.should raise_error(NoMethodError)
end
+
+ ruby_version_is "2.7" do
+ it "raises an ArgumentError when the key exists" do
+ os = OpenStruct.new(test: 20)
+ lambda { os.method_missing(:test, 1, 2, 3) }.should raise_error(ArgumentError)
+ end
+ end
end
describe "OpenStruct#method_missing when not passed any additional arguments" do