---
title: "Interfaces"
url: https://docs.zeroc.com/ice/3.8/slice/interfaces
language: ruby
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Interfaces

Programming language: Ruby. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/interfaces/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/interfaces/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/interfaces/java.md), [JavaScript](https://docs.zeroc.com/ice/3.8/slice/interfaces/js.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/interfaces/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/interfaces/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/interfaces/python.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/interfaces/swift.md).

## Syntax and Semantics of Interfaces

A Slice interface defines the operations that clients invoke on an Ice object. For example:

```slice
module M
{
    struct TimeOfDay
    {
        short hour;         // 0 - 23
        short minute;       // 0 - 59
        short second;       // 0 - 59
    }

    interface Clock
    {
        TimeOfDay getTime();
        void setTime(TimeOfDay time);
    }
}
```

`Clock` defines two operations: `getTime` returns the current time, and `setTime` sets it. A client calls these operations on a proxy for an Ice object that implements `Clock`.

An interface contains only operations: you cannot define a type, an exception, or a field inside an interface.

An interface can be empty:

```slice
interface Empty {}
```

## Client-Side Mapping for Interfaces

### Proxy Classes

On the client side, a Slice interface maps to a Ruby class with methods that correspond to the operations on those interfaces. Consider the following Slice interface:

```slice
module M
{
    interface Simple
    {
        void op();
    }
}
```

The Ruby mapping generates the following definition for use by the client:

```ruby
module SimplePrx_mixin
    def op(context=nil)
        ...
    end
end

class SimplePrx < Ice::ObjectPrx
    include SimplePrx_mixin
end
```

In the client, an instance of `SimplePrx` is a proxy for an Ice object that implements `Simple`: it holds the addressing information of this object, such as its identity and endpoints; see [Proxy Types](https://docs.zeroc.com/ice/3.8/slice/interfaces/proxy-types.md).

### Creating a Proxy

Use the constructor of the generated class to create a proxy from a communicator and a “stringified” proxy. For example:

```ruby
simple = M::SimplePrx.new(communicator, "simple:tcp -h localhost -p 4061")
```

### Inheritance from `Ice::ObjectPrx`

All generated proxy classes inherit directly or indirectly from the `Ice::ObjectPrx` class, reflecting the fact that all Slice interfaces implicitly inherit from `Object`.

### Interface Inheritance

Inheritance relationships among Slice interfaces are maintained in the generated Ruby classes. For example:

```slice
interface A { ... }
interface B { ... }
interface C extends A, B { ... }
```

The generated code for `CPrx` uses mixins to include the operations of `APrx` and `BPrx`:

```ruby
module CPrx_mixin
    include APrx_mixin
    include BPrx_mixin
end

class CPrx < Ice::ObjectPrx
    include CPrx_mixin
end
```

Given a proxy for `C`, a client can invoke any operation defined for interface `C`, as well as any operation inherited from `C`'s base interfaces.

### Creating a Proxy of Another Type

All proxy classes provide 2 class methods that allow you to convert any proxy into a proxy of this type.

```ruby
def uncheckedCast(proxy, facet: nil)
    ...
end

def checkedCast(proxy, facet: nil, context: nil)
   ...
end
```

#### uncheckedCast

The `uncheckedCast` method allows you to convert any proxy into a proxy of this type. For example:

```ruby
# Convert a SimplePrx into a WidgetPrx, even though the two types are unrelated.
widget = M::WidgetPrx.uncheckedCast(simple)
```

`uncheckedCast` is a local operation that always succeeds.

#### checkedCast

`checkedCast` makes a remote call to the target object to check whether it implements the Slice interface of the requested proxy type. For example:

```ruby
# Call operation ice_isA on the Ice object to check if it implements Slice interface
# Widget.
widget = M::WidgetPrx.checkedCast(simple)
```

If the target object implements the Slice interface, `checkedCast` returns a new proxy, just like `uncheckedCast`. If the target object doesn’t implement this interface, `checkedCast` returns nil. `checkedCast` can also raise an exception, for example if it cannot reach the remote object.

Use `uncheckedCast` when you know the target object implements the Slice interface, and `checkedCast` when you need to ask the target object whether it implements this interface. You rarely need either: you usually [create a proxy](https://docs.zeroc.com/ice/3.8/runtime/invocation/creating-proxies/ruby.md#creating-a-proxy-of-another-type) of the type you need directly.

### Proxy Factory Methods

The base proxy class `ObjectPrx` supports a variety of methods for customizing a proxy. Since proxies are immutable, each of these factory methods returns a copy of the original proxy that contains the desired modification. For example, you can obtain a proxy configured with a ten second invocation timeout as shown below:

```ruby
greeter = VisitorCenter::GreeterPrx.new(
    communicator,
    "greeter:tcp -h localhost -p 4061")

# Create a new GreeterPrx and assign it to greeter.
greeter = greeter.ice_invocationTimeout(10000)
```

The proxy factory methods usually return a proxy of the same type as the current proxy, like in the example above.

The only exceptions are the factory methods `ice_facet` and `ice_identity`. Calls to either of these methods may produce a proxy for an object of an unrelated type, and you need to convert the returned proxy into a proxy of that type. For example:

```ruby
greeter = VisitorCenter::GreeterPrx.new(
    communicator, "greeter:tcp -h localhost -p 4061")
greeterAdmin = VisitorCenter::GreeterAdminPrx.uncheckedCast(
    greeter.ice_facet("admin"))
```

## See Also

- [Operations](https://docs.zeroc.com/ice/3.8/slice/operations/ruby.md)
- [User Exceptions](https://docs.zeroc.com/ice/3.8/slice/exceptions/ruby.md)
