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

# Structures

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

## Struct Syntax

A structure groups one or more named fields, of any type, into a new type. For example:

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

This definition introduces a new type named `TimeOfDay`. The name of a field must be unique within its structure.

A structure contains only fields: as elsewhere in Slice, type definitions cannot be nested, except for [modules](https://docs.zeroc.com/ice/3.8/slice/modules/ruby.md). Like every other type, you define a structure in a module.

## Language Mapping

A Slice structure maps to a Ruby class with the same name, capitalized. For each Slice field, the class contains an instance variable with read and write accessors. Consider this structure:

```slice
struct Employee
{
    long number;
    string firstName;
    string lastName;
}
```

The Ruby mapping generates the following definition for this structure:

```ruby
class Employee
    attr_accessor :number, :firstName, :lastName

    def initialize(number=0, firstName='', lastName='')
        @number = number
        @firstName = firstName
        @lastName = lastName
    end

    def hash
        # ...
    end

    def ==(other)
        # ...
    end

    def inspect
        # ...
    end
end
```

The compiler generates a definition for the `hash` method, which allows instances to be used as keys in a hash collection. The `hash` method returns a hash value for the structure based on the value of its instance variables.

The `==` method returns true if all instance variables of two structures are (recursively) equal.

The `inspect` method returns a string representation of the structure.

### 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 attributes).

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

## See Also

- [Fields](https://docs.zeroc.com/ice/3.8/slice/fields/ruby.md)
- [Classes](https://docs.zeroc.com/ice/3.8/slice/user-defined-types/classes/ruby.md)
