---
title: "Classes"
url: https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes
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).

# Classes

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

A class is a user-defined type that holds a list of fields, like a [struct](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/structures/ruby.md). You define a class with the `class` keyword:

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

Unlike a struct, a class can be empty.

Classes offer capabilities that structs do not:

- **Extensibility** You can extend a class through [inheritance](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/class-inheritance.md) and [optional fields](https://docs.zeroc.com/ice/3.8/slice/fields/ruby.md#optional-fields).
- **Graph preservation** Ice preserves the references between class instances: two references to the same instance in a [class graph](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/self-referential-classes/ruby.md) still refer to one instance after unmarshaling.
- **Null value** A class parameter or field can be null, whereas a struct parameter or field cannot.
- **Slicing** With the [sliced format](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/slicing-values-and-exceptions/ruby.md#slice-formats), a receiver can unmarshal an instance of a derived class it does not know as the most-derived base class it knows.

Marshaling a class takes more time and space than marshaling a struct, so use a class only when you need one of these capabilities.

> **Note**
>
> A class represents data that you transmit over the wire, just like a struct. You can't define operations on a Slice class or implement an interface with a Slice class.

## Language Mapping

A Slice class maps to a Ruby class with the same name, capitalized. For each Slice field, the generated class contains an instance variable with read and write accessors, as for structures and exceptions. Consider the following class definition:

```slice
class TimeOfDay
{
    short hour;         // 0 - 23
    short minute;       // 0 - 59
    short second;       // 0 - 59
}
```

The Ruby mapping generates the following code for this definition:

```ruby
class TimeOfDay < ::Ice::Value
    attr_accessor :hour, :minute, :second

    def initialize(hour=0, minute=0, second=0)
        @hour = hour
        @minute = minute
        @second = second
    end
end
```

There are a number of things to note about the generated code:

1. The generated class `TimeOfDay` derives from `Ice::Value`. This reflects the semantics of Slice classes in that all classes implicitly inherit from `Ice::Value`, which is the ultimate ancestor of all classes.
2. The constructor defines an instance variable for each Slice field.

### Generated Constructor

The generated constructor has one parameter for each field. This allows you to construct and initialize an instance in a single statement (instead of first having to construct the instance and then assign to its variables).

All these parameters have also default values (see [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/ruby.md)).

For derived classes, the constructor has one parameter for each of the base class's fields, plus one parameter for each of the derived class's fields, in base-to-derived order.
