Undefined method

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

On Aug 26, 2008, at 7:08 PM, iga wrote:

(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.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Thanks,

This appears to have worked. When you suggest the need to “be careful”
what did you mean?

+bm

On Aug 26, 6:14 pm, Rob B. [email protected]

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.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs