Bug #19301
closedFix Data class to report keyrest instead of rest parameters
Description
Overview¶
Hello and Happy New Year. 👋
With the new Data class, I'm seeing a discrepancy in parameter behavior compared to a Struct. I understand the original Data feature request made design choices to only accept keyword arguments for the #initialize method but reporting [[rest]] parameters seems misleading to me because it doesn't share the more flexible Struct#initialize behavior and would like to request Data#initialize answer [[keyrest]] for parameters for improved metaprogramming accuracy.
Steps to Recreate¶
To reproduce, consider the following:
DataExample = Data.define :one, :two
StructExample = Struct.new :one, :two
argument_array = [one: 1, two: 2]
argument_hash = {one: 1, two: 2}
puts "Data (parameters): #{DataExample.method(:initialize).parameters}"
puts "Struct (parameters): #{StructExample.method(:initialize).parameters}"
puts "Data (argument hash): #{DataExample[**argument_hash]}"
puts "Struct (argument array): #{StructExample[*argument_array]}"
puts "Struct (argument hash): #{StructExample[**argument_hash]}"
The above will output the following:
Data (parameters): [[:rest]]
Struct (parameters): [[:rest]]
Data (argument hash): #<data DataExample one=1, two=2>
Struct (argument array): #<struct StructExample one={:one=>1, :two=>2}, two=nil>
Struct (argument hash): #<struct StructExample one=1, two=2>
The Struct class -- as far as I know -- has always reported [[rest]] parameters even though it can accept positional or keyword arguments without error. ...but this is definitely not the case with the Data class which can be seen when running the following modification to the above:
DemoExample[*argument_array]
# missing keyword: :two (ArgumentError)
The above clearly betrays the [[rest]] parameters response (granted a Struct is slightly devious too but at least happily accepts positional or keyword arguments). With this in mind, could Data#initalize be fixed to at least report [[keyrest]] so we'd have a better chance of metaprogramming the correct argument format based on the #parameters response for initializing a Data instance correctly?
Thanks. 🙇🏻♂️
Environment¶
ruby 3.2.0 (2022-12-25 revision a528908271) [arm64-darwin22.2.0]