ruby-changes:58507
From: Alan <ko1@a...>
Date: Thu, 31 Oct 2019 00:38:16 +0900 (JST)
Subject: [ruby-changes:58507] 4c7f789e94 (master): Allow only one argument for keyword_init struct
https://git.ruby-lang.org/ruby.git/commit/?id=4c7f789e94 From 4c7f789e942e78ebd3a7e3bf458c6cbe2133d692 Mon Sep 17 00:00:00 2001 From: Alan Wu <XrXr@u...> Date: Tue, 29 Oct 2019 20:08:01 -0400 Subject: Allow only one argument for keyword_init struct ``` irb(main):001:0> RUBY_VERSION => "2.6.5" irb(main):002:0> S = Struct.new(:foo, keyword_init: true) => S(keyword_init: true) irb(main):003:0> S.new({foo: 23424}, 234) # I don't think this is intentional => #<struct S foo=23424> irb(main):004:0> ``` Tightening this up should inform users when they are confused about whether a struct is `keyword_init`. diff --git a/struct.c b/struct.c index 8294e9f..273c719 100644 --- a/struct.c +++ b/struct.c @@ -633,7 +633,7 @@ rb_struct_initialize_m(int argc, const VALUE *argv, VALUE self) https://github.com/ruby/ruby/blob/trunk/struct.c#L633 n = num_members(klass); if (argc > 0 && RTEST(rb_struct_s_keyword_init(klass))) { struct struct_hash_set_arg arg; - if (argc > 2 || !RB_TYPE_P(argv[0], T_HASH)) { + if (argc > 1 || !RB_TYPE_P(argv[0], T_HASH)) { rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 0)", argc); } rb_mem_clear((VALUE *)RSTRUCT_CONST_PTR(self), n); diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb index ec7728b..22a6ce8 100644 --- a/test/ruby/test_struct.rb +++ b/test/ruby/test_struct.rb @@ -105,6 +105,7 @@ module TestStruct https://github.com/ruby/ruby/blob/trunk/test/ruby/test_struct.rb#L105 @Struct.new("KeywordInitFalse", :a, :b, keyword_init: false) assert_raise(ArgumentError) { @Struct::KeywordInitTrue.new(1, 2) } + assert_raise(ArgumentError) { @Struct::KeywordInitTrue.new({a: 100}, 2) } assert_nothing_raised { @Struct::KeywordInitFalse.new(1, 2) } assert_nothing_raised { @Struct::KeywordInitTrue.new(a: 1, b: 2) } assert_raise(ArgumentError) { @Struct::KeywordInitTrue.new(1, b: 2) } -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/