Project

General

Profile

Feature #16356

Method#inspect of argument forwarding

Added by znz (Kazuhiro NISHIYAMA) 24 days ago. Updated 23 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:95896]

Description

Current behavior includes (**, &&).
Is this intentional?

% ruby -e 'def mf(...);end;p method(:mf)'
#<Method: main.mf(**, &&) -e:1>

I added tests of current behavior at https://github.com/ruby/ruby/commit/777973084e599cf9efa490173709b187fb507f90


Related issues

Related to Ruby master - Feature #14145: Proposal: Better Method#inspectOpenActions

Associated revisions

Revision 77797308
Added by znz (Kazuhiro NISHIYAMA) 24 days ago

Add tests of argument forwarding's parameters and inspect

Revision 9b52bacc
Added by nobu (Nobuyoshi Nakada) 23 days ago

Refined inspection of argument forwarding

History

#1

Updated by znz (Kazuhiro NISHIYAMA) 24 days ago

Updated by zverok (Victor Shepelev) 23 days ago

#inspect just follows whatever #parameters say.

But I indeed find that #parameters return value is kinda weird for this case:

./ruby --disable-gems -e "def m(...); end; p method(:m).parameters"
[[:rest, :*], [:block, :&]]

E.g., literally, "parameter of the type :rest, named *, and parameter of the type :block, named &".

I believe that a proper return value for parameters in this case would be, probably

[[:rest], [:keyrest], [:block]]

If it would be so (and it seems reasonable), #inspect will return #m(*, **, &)

Updated by jeremyevans0 (Jeremy Evans) 23 days ago

zverok (Victor Shepelev) wrote:

#inspect just follows whatever #parameters say.

But I indeed find that #parameters return value is kinda weird for this case:

./ruby --disable-gems -e "def m(...); end; p method(:m).parameters"
[[:rest, :*], [:block, :&]]

E.g., literally, "parameter of the type :rest, named *, and parameter of the type :block, named &".

I believe that a proper return value for parameters in this case would be, probably

[[:rest], [:keyrest], [:block]]

If it would be so (and it seems reasonable), #inspect will return #m(*, **, &)

That's not how ... is implemented, though. It is implemented so that:

def a(...)
  b(...)
end

means

ruby2_keywords def a(*args, &block)
  b(*args, &block)
end

other than the local variable names. So the current behavior omitting :keyrest makes sense. The local variable names should not be included, so the parameters output should probably be [[:rest], [:block]].

This does raise a question of whether methods flagged with ruby2_keywords should have their parameters output reflect that. I'm not sure whether that is worth doing, but if so, we should probably replace :rest with something like :restkw.

Updated by zverok (Victor Shepelev) 23 days ago

jeremyevans0 (Jeremy Evans) thanks for the explanation, I suspected there is something important about missing :keyrest there :)
But names (e.g. literal :* and :&) should be excluded from parameters output anyways, right?..

Updated by shevegen (Robert A. Heiler) 23 days ago

But names (e.g. literal :* and :&) should be excluded from parameters output anyways, right?

I am not matz, nor among the core team, but I think that the general intention is that the
information displayed here (e. g. via #inspect) should be useful to the user, to some extent;
and ideally consistent/uniform whenever possible.

As Kazuhiro showed, the display is like:

#<Method: main.mf(**, &&) -e:1>

And I am not sure if this is that helpful for ruby users. It is also a bit strange that
-e:1 is passed. I can not imagine that this is very useful to anyone. :) This is from
the commandline, yes? I remember having that sometimes when invoking ruby scripts or
e. g. calling a custom method in some .rb file.

So, no matter whether there may be reasons behind the display, I think Kazuhiro's question
is a good one either way how you look at this. (Actually what may be missing in the
issue description is to say what should otherwise be shown, rather than the current
"main.mf(**, &&) -e:1". I guess one could also show more information if wanted or
necessary, such as to indicate that it may be a forwarded-message. Perhaps matz or
nobu may know what might fit best here.)

So the current behavior omitting :keyrest makes sense.

I think this is only one part; the other is whether this is very useful for ruby
users. Kazuhiro did not explicitely mention this, but I think the current display
is a bit weird.

Also available in: Atom PDF