Question
When in a method, how can I pass on all parameters provided as part of the method sending to another method sending? The parameters should all be named in the sense that there are no placeholders/catchalls like *args
but they can be a mix of keyword arguments and non keyword arguments. Additionally, the inner method send is not super
.
Example (pseudocode)
def some_method(a_parameter, a_named_parameter:)
...do something...
some_other_method([send with original parameters])
...do something else...
end
Related question
Is there a way to access method arguments in Ruby? has been asked 7 years ago.
Ugly hack
Based on what I have found, it is possible to do something like this for keyword parameters:
def some_method(a_named_parameter:, another_named_parameter:)
...do something...
params = method(__method__)
.parameters
.map { |p| [p[1], binding.local_variable_get(p[1])] }
.to_h
some_other_method(**params)
...do something else...
end
And this for non-keyword parameters:
def some_method(a_named_parameter, another_named_parameter)
...do something...
params = method(__method__)
.parameters
.map { |p| binding.local_variable_get(p[1]) }
some_other_method(*params)
...do something else...
end
Based on the information returned by method(__method__).parameters
one could also work out a solution that would work for both, but even given that it would be possible to extract all of this into a helper, it is way to complicated.