I'm working on a bit of metaprogramming using send
methods quite a bit. I've been successful so far because the methods I'm send
ing to only take one argument.
Example:
client
is an API client
@command
is a method on client
taken as an option to a CLI utility
@verb
is a method on command
taken as another option in the CLI
def command_keys
case @command
when "something"
self.command_options.slice(:some, :keys)
end
end
Then I call the API client like this:
client.send(@command).send(@verb, command_keys)
This works since the methods all take a Hash
as their argument. The problem I've run into is when I need to send more than 1 parameter in command_keys
. What I'm wondering is the best way to handle the command_keys
method returning more than 1 value. Example:
def command_keys
case @command
when "something"
return self.command_options[:some], self.command_options[:keys]
end
end
In this case, command_keys
returns an Array as expected, but when I try to pass that in the send(@verb, command_options)
call, it passes it as an Array (which is obviously expected). So, to make a long story short, is there some easy way to make this condition be handled easily?
I know send(@verb, argument1, argument2)
would get me the result I want, but I would like to be able to not have to give my script any more implementation logic than it needs, that is to say I would like it to remain as abstracted as possible.
Use splat. You might have to rethink the code a bit, but something like:
client.send(@command).send(@verb, *all_the_args)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.