ruby-changes:55348
From: marcandre <ko1@a...>
Date: Sun, 14 Apr 2019 22:47:30 +0900 (JST)
Subject: [ruby-changes:55348] marcandRe: r67556 (trunk): OpenStruct: improve error message when passing wrong number of arguments.
marcandre 2019-04-14 22:47:24 +0900 (Sun, 14 Apr 2019) New Revision: 67556 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=67556 Log: OpenStruct: improve error message when passing wrong number of arguments. Patch by Lisa Ugray (issue #15515) Modified files: trunk/lib/ostruct.rb trunk/spec/ruby/library/openstruct/method_missing_spec.rb Index: spec/ruby/library/openstruct/method_missing_spec.rb =================================================================== --- spec/ruby/library/openstruct/method_missing_spec.rb (revision 67555) +++ spec/ruby/library/openstruct/method_missing_spec.rb (revision 67556) @@ -32,10 +32,17 @@ describe "OpenStruct#method_missing when https://github.com/ruby/ruby/blob/trunk/spec/ruby/library/openstruct/method_missing_spec.rb#L32 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 Index: lib/ostruct.rb =================================================================== --- lib/ostruct.rb (revision 67555) +++ lib/ostruct.rb (revision 67556) @@ -213,7 +213,7 @@ class OpenStruct https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L213 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 https://github.com/ruby/ruby/blob/trunk/lib/ostruct.rb#L221 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/