Client-Side Ruby Mapping for Operations
Mapping for Operations
As we saw in the Client-Side Ruby Mapping for Interfaces, for each operation on an interface, the generated proxy class contains a method with the same name. To invoke an operation, you call this method on the proxy. For example, let’s take the generated code from the greeter example:
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}
The proxy class generated from the Greeter interface, after removing extra details, is as follows:
module ::VisitorCenter
module GreeterPrx_mixin
def greet(name, context=nil)
GreeterPrx_mixin::OP_greet.invoke(self, [name], context)
end
end
class GreeterPrx < Ice::ObjectPrx
include GreeterPrx_mixin
end
# ...
end
Given a proxy to an object of type Greeter, the client can invoke the greet operation as follows:
greeter = VisitorCenter::GreeterPrx.new(
communicator, "greeter:tcp -h localhost -p 4061")
greeting = greeter.greet("Alice") # Get name via RPC
Exception Handling
Any operation invocation may throw a runtime exception and, if the operation has an exception specification, may also throw user exceptions. Suppose we have the following simple interface:
exception Tantrum
{
string reason;
}
interface Child
{
void askToCleanUp() throws Tantrum;
}
Slice exceptions are thrown as Ruby exceptions, so you can simply enclose one or more operation invocations in a begin-rescue block:
child = ... # Get child proxy...
begin
child.askToCleanUp()
rescue Tantrum => t
puts "The child says: #{t.reason}"
end