Project

General

Profile

Actions

Bug #18632

closed

Struct.new wrongly treats a positional Hash as keyword arguments

Added by Eregon (Benoit Daloze) over 2 years ago. Updated over 2 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux]
[ruby-core:107893]

Description

$ ruby -e 'Struct.new(:a, name: "b")'   
-e:1:in `new': unknown keyword: :name (ArgumentError)
	from -e:1:in `<main>'
^ expected

$ ruby -e 'Struct.new(:a, { name: "b" })'
-e:1:in `new': unknown keyword: :name (ArgumentError)
	from -e:1:in `<main>'
^ 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0