From: "bkuhlmann (Brooke Kuhlmann) via ruby-core" Date: 2023-01-04T16:48:29+00:00 Subject: [ruby-core:111627] [Ruby master Feature#8088] Method#parameters (and friends) should provide useful information about core methods Issue #8088 has been updated by bkuhlmann (Brooke Kuhlmann). I've been bitten by this same issue (see #19301) in Ruby 3.2.0 with the introduction of the new [Data](https://www.alchemists.io/articles/ruby_data) class. At the time of opening that issue, I didn't fully realize how *untruthful* `Method#parameters` is with C-based implementations since it answers `[[rest]]` for parameters which is incorrect and *very hard* to *dynamically* build the *correct* argument list when given wrong parameters. In case it helps, here's a code snippet that demonstrates the issue when using only `Data` and `Struct` objects: ``` ruby DataExample = Data.define :one, :two StructAny = Struct.new :one, :two StructKeywordOnly = Struct.new :one, :two, keyword_init: true models = [DataExample, StructAny, StructKeywordOnly] arguments = [{one: 1, two: 2}] models.each do |model| puts "#{model}#initialize parameters: #{model.method(:initialize).parameters}" end puts models.each do |model| print "#{model}: " puts model[*arguments] rescue ArgumentError => error puts error.message end # DataExample#initialize parameters: [[:rest]] # StructAny#initialize parameters: [[:rest]] # StructKeywordOnly#initialize parameters: [[:rest]] # # DataExample: missing keyword: :two # StructAny: #1, :two=>2}, two=nil> # StructKeywordOnly: # ``` In all three models, messaging `model.method(:initialize).parameters` will always answer `[[rest]]` for parameters so when I build my arguments (i.e. `[{one: 1, two: 2}]`) in the same format as dictated from the `Method#parameters` then the only model that gives me the correct instance is the `StructKeywordOnly` model because using `keyword_init: true` does that coercion for me. ���� My C knowledge is pretty terrible so I don't know how hard this would be to fix but would definitely welcome having accurate and truthful `Method#parameters` information for C-based implementations. ---------------------------------------- Feature #8088: Method#parameters (and friends) should provide useful information about core methods https://bugs.ruby-lang.org/issues/8088#change-100998 * Author: headius (Charles Nutter) * Status: Open * Priority: Normal ---------------------------------------- I was wiring up #parameters to work for native methods today when I realized MRI doesn't give very good information about variable-arity native methods: ext-jruby-local ~/projects/jruby $ ruby2.0.0 -e "p ''.method(:gsub).to_proc.parameters" [[:rest]] ext-jruby-local ~/projects/jruby $ jruby -e "p ''.method(:gsub).to_proc.parameters" [[:req], [:opt]] I think MRI should present the same as JRuby here; gsub is obviously not a rest-arg method and you can't call it with less than 1 or more than 2 arguments. JRuby's presenting the right output here. I'm probably going to have to change JRuby to do the less-helpful version so we're compliant and tests pass, but I think the specification of #parameters should be that it presents the JRuby version about rather than the MRI version. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/