[ruby-core:102320] [Ruby master Bug#17596] Calling parameters on `new` method results in :rest and -1 arity
From:
matsumoto@...
Date:
2021-01-30 08:57:25 UTC
List:
ruby-core #102320
Issue #17596 has been updated by soutaro (Soutaro Matsumoto).
Hi Brandon,
It is because `Testing.new` is `Class#new`, which accepts any arguments and forward to `Testing#initialize`. (I'm not sure if we can change the behavior.)
A workaround would be using `instance_method(:initialize)`.
``` ruby
Testing.method(:new).owner # => Class
Testing.method(:initialize).owner # => Class
Testing.instance_method(:initialize).owner # => Testing
```
----------------------------------------
Bug #17596: Calling parameters on `new` method results in :rest and -1 arity
https://bugs.ruby-lang.org/issues/17596#change-90173
* Author: baweaver (Brandon Weaver)
* Status: Open
* Priority: Normal
* ruby -v: 3.0.0
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN
----------------------------------------
Consider the following class:
```ruby
class Testing
def initialize(a, b, c)
# ...
end
end
```
...and the following call:
```ruby
m = Testing.method(:new)
p arity: m.arity, params: m.parameters
```
**Expected**
```ruby
{ arity: 3, params: [[:req, :a], [:req, :b], [:req, :c]] }
```
**Actual**
```ruby
{ arity: -1, params: [[:rest]] }
```
This is confusing to me as I would expect to be able to see the parameters of the `new` method like this. The same applies for `initialize.
I was experimenting with pattern matching interfaces and dynamically defining class constructor definitions as such:
```ruby
def deconstruct
method(:new).parameters.map { public_send(_1.last) }
end
```
While this case is not 100% safe for all initializers I had expected it to work for testing and was surprised when it did not.
I've tested and confirmed this behavior back to 2.5 and tested no further back
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>