Skip to main content
Skip table of contents

Writing a Greeter Client in Ruby

This page presents a step-by-step guide to writing the client-side of our Ruby Greeter application.

This client creates a proxy to a remote object that implements the Greeter interface, and invokes the greet operation on this object.

You can find the complete source code for this example in the ice-demos repository.

Compile Slice File with Slice Compiler

The first step when writing a Ruby application with Ice is to compile the Slice definitions for this application with the Slice to Ruby compiler (slice2rb).

Here, we compile the Greeter.ice Slice file we wrote earlier in a terminal:

BASH
slice2rb Greeter.ice

This compilation produces a Ruby source file, Greeter.rb. This file provides the proxy class that we’ll use in our client code, so it’s essential to generate this file at the beginning of the development process.

Client Script

Our client is a small Ruby script that loads the Ice library and the Greeter.rb file generated by the Slice compiler:

RUBY
#!/usr/bin/env ruby

require 'Ice'

require_relative 'Greeter.rb'

The remainder of this script can be broken down into four pieces:

1. Create a Communicator

First, we create a Communicator using Ice::initialize:

MATLAB
Ice::initialize(ARGV) do |communicator|
    ...
end

The communicator is the main entry point into the Ice runtime. Its responsibilities include establishing connections to servers, caching these connections, and managing configuration properties. We also need a communicator to create a proxy (see next step).

Our client, like most Ice applications, creates a single communicator.

When we no longer need a communicator, we must call destroy on this communicator. This destruction closes network connections and performs other important cleanups. We use Ruby’s “execute-around” pattern to destroy the communicator at the end of the block.

2. Create a Greeter Proxy

Next, we need a way to call on a remote Greeter object. In Ice, this is done with proxies. Proxies are local constructs that represent remote Ice objects and provide methods to call operations on those objects.

We create a Greeter proxy by constructing an instance of the GreeterPrx class generated by the Slice compiler:

RUBY
greeter = VisitorCenter::GreeterPrx.new(
    communicator, 
    "greeter:tcp -h hello.zeroc.com -p 4061")

The constructor accepts our communicator and a “stringified proxy” with the address of the remote Ice object. Here, our stringified proxy says the target Ice object is named “greeter” and can be reached via tcp on hello.zeroc.com, on port 4061.

The name of the interface (Greeter) and the identity of the Ice object (greeter) are independent. The Ice objects hosted in the server could just as easily have identities like santa, bugsBunny, etc.

3. Make an Invocation

The third step is to call greet on the remote Ice object using our proxy, and print the greeting:

RUBY
greeting = greeter.greet("alice")
puts greeting

The greet method does all the heavy lifting for us: the proxy creates a request with the user name (“alice”), the communicator establishes a connection to hello.zeroc.com:4061 and sends the request over this connection. Later, the communicator receives the response, the proxy unmarshals the response’s payload and returns a string (the greeting).

Here, we make a synchronous invocation, which means this call blocks until we receive the greeting. Ice for Ruby supports only synchronous invocations; other languages support asynchronous invocations as well.

4. Cleanup

The final step is the end of our block. At this point the communicator is destroyed, and our script completes.

Running the Client

We can now run this script in the Ruby console:

BASH
ruby client.rb

Ice for Ruby supports only client development.

Here, we connect to the Greeter server running on hello.zeroc.com. This Ice server is implemented in a language with server-side support (C++, C#, Java, Python, or Swift).

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.