From: "jeremyevans0 (Jeremy Evans)" Date: 2022-03-14T16:21:45+00:00 Subject: [ruby-core:107897] [Ruby master Bug#18632] Struct.new wrongly treats a positional Hash as keyword arguments Issue #18632 has been updated by jeremyevans0 (Jeremy Evans). Eregon (Benoit Daloze) wrote in #note-3: > You mean rb_scan_args/rb_scan_args_kw correctly separate positional & kwargs, but the rest do not? Correct. I think most C functions that handle kwargs use `rb_scan_args`, but not all. I recommend you add this as topic to the next developer meeting. Changes in this area will break backwards compatibility, so even if we decide to make them, we need to have an implementation plan, such as issuing deprecation warnings in 3.2 and breaking use with positional hashes in 3.3. I'm willing to do the work of auditing all core/ext methods and updating those that need changes if we decide to make this change. ---------------------------------------- Bug #18632: Struct.new wrongly treats a positional Hash as keyword arguments https://bugs.ruby-lang.org/issues/18632#change-96836 * Author: Eregon (Benoit Daloze) * Status: Open * Priority: Normal * ruby -v: ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux] * Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- ``` $ ruby -e 'Struct.new(:a, name: "b")' -e:1:in `new': unknown keyword: :name (ArgumentError) from -e:1:in `
' ^ expected $ ruby -e 'Struct.new(:a, { name: "b" })' -e:1:in `new': unknown keyword: :name (ArgumentError) from -e:1:in `
' ^ wrong ``` It shouldn't be such an error for the 2nd command since it's a positional Hash. It should be a TypeError, like when passing e.g. `nil` instead of the positional Hash. Also: ``` $ ruby -e 'p Struct.new(:a, {}).members' [:a] ``` But it should be an error to pass a positional Hash. I think this is worth fixing, because it basically breaks the separation of positional and keyword arguments for this method. Also Struct.new does take a keyword argument, `keyword_init: true`. -- https://bugs.ruby-lang.org/ Unsubscribe: