I’m trying to pass a method to another object/method as a variable.
Example one, login 1 works just fine.
def login1 @driver.Login(username, password)
end
However, if I try to pass “Login” to the driver as an argument
(example two Login 2), I get “undefined method `command_string’ for
#SOAP::RPC::Driver:0x316dc28”
“Login” is a legit method for the SOAP::RPC::Driver. Do I need to do
something to variable first?
def login2
command_string = “Login”
send_to_api(command_string)
end
def send_to_api(command_string)
results = @driver.command_string(username, password)
end
(example two Login 2), I get "undefined method `command_string’ for
def send_to_api(command_string)
results = @driver.command_string(username, password)
end
While you need to be careful, you can do exactly that by using send.
def send_to_api(command_string) @driver.send(command_string, username, password)
end
There’s no need to assign to results, the last expression evaluated is
the return value of the method.
Through the use of Object#send any method can be invoked – even those
that are protected or private. You usually need to have a good reason
to circumvent the restrictions put in place by the class author.
For a SOAP application, you’re responsible for making sure that the
methods that are called in this way are the “right” ones.