OpenStruct in Ruby

Mehdi Farsi
Aug 22, 2018 · 4 min read

In this article, we’re going to explore the following topics:

  • the OpenStruct class
  • data structure: creation and manipulation
  • OpenStruct and lazy loading
  • data structures behind the scene

Feel free to read the Struct in Ruby article if you are unfamiliar with data structures in Ruby. This will give you a better comprehension of the technical notions of this article.

The OpenStruct class

The OpenStruct class allows to create flexible data structures.

Indeed, the structure doesn’t need to provide a rigid list of members as it doesn’t define any structure type but data structures that you fill in afterwards.

It provides a pair of getter/setter methods for each attribute.

This is similar to the attr_accessor method for classes.

Feel free to read the Attributes in Ruby article if you are unfamiliar with the attr_* methods in Ruby.

This class is part of the Ruby’s Standard Library (a.k.a the stdlib).

Let’s have a look to the OpenStruct’s ancestor chain

irb> require 'ostruct'
=> true
irb> OpenStruct.ancestors
=> [OpenStruct, Object, Kernel, BasicObject]

As it’s not part of the Ruby Core, we have to require the ostruct library which contains the OpenStruct class definition.

The OpenStruct inherits from Object— which is the default parent class in Ruby.

Note that it doesn’t include any specific module.

Indeed, unlike the Struct class, it doesn’t include the Enumerable module.

Feel free to read the Ruby Object Model article if you are unfamiliar with the Object class and the ancestor chain.

Feel free to read the The Enumerable module in Ruby: Part I article if you are unfamiliar with the Enumerable module.

Data structure: creation and manipulation

The OpenStruct::new is in charge of creating new data structure.

This method accepts a Hash, a Struct or an OpenStruct as parameter

Firstly, we instantiate a new OpenStruct object with one attribute called ram and we store this object in the computer variable.

Then we access the ram attribute by 3 different ways:

  • computer.ram: the ram accessor method
  • computer[:ram]: the OpenStruct#[] with a symbol key
  • computer['ram']: the OpenStruct#[] with a string key

Next, we define the screens attribute.

Notice that there is 3 ways to define a new attribute or to modify the value of an existing one:

  • computer.screens=: the screens= accessor method
  • computer[:screens]=: the OpenStruct#[]= with a symbol key
  • computer['screens']=: the OpenStruct#[]= with a string key

Finally, we access the value of the screens attribute the same way as the ram attribute.

Unlike the Struct class that defines a structure type with a rigid attributes list, the OpenStruct class defines a new data structure that can add attributes on-the-fly — like the screen attribute in the above example.

OpenStruct and lazy loading

In order to save memory and speed up the access to an attribute, the accessor methods of an attribute are lazy loaded at certain points.

This means that the methods are defined only when a set of defined actions are triggered.

This allows your program to only define the minimum amount of required methods to make your data structure works.


After the OpenStruct initialization, we see that the computer.ram and computer.ram= accessor methods are not defined yet.

Indeed, we can see that after a call to computer.ram, the computer.ram and computer.ram= methods are now defined.

Note that a call to computer[:ram] doesn’t define the accessor methods.

Then we set a value to the :screens attribute via the OpenStruct#[]= method.

After this method call, the computer.screens and computer.screens= accessors are defined.

To recap, accessor methods are defined only when:

  • an accessor method of an existing (or not) attribute is called for the first time.
  • the OpenStruct#[]= method is called for the first time — computer[:screens] = 2 for example.

Data structures behind the scene

Now that we are more familiar with the OpenStruct class, let’s dive into what happens behind the scene when we create and manipulate an OpenStruct object.

Each data structure from an OpenStruct defines an internal hash that contains a mapping table for each attribute and value.

This container is commonly called the table

Note that each attribute name is converted to a Symbol before to be included into the table.

Now let’s have a look to the evolution of the computer ‘s table

Here the table of the computer’s data structure is { ram: "4GB" }.

Now let’s add the screens attribute to the computer object

At this point the content of the table is { ram: "4GB", screens: 2 }.

What happens when an attribute that doesn’t exist is called?

For example, a call to the computer.cores= setter method

When a method is not defined within the entire ancestor chain of a given object then Ruby automatically calls the first defined method_missing Hook Method in this ancestor chain.

The OpenStruct defines its own version of the method_missing hook method.

So when computer.cores = 2 is called then the OpenStruct#method_missing method is called behind the scene.

Let’s detail the execution flow of this method when the computer.cores = 2 is called:

  • 1/ getter and setter methods are defined and the key passed as parameter is converted into a Symbol:cores
  • 2/ the value is inserted into the internal hash using this key — @table[:cores] = 2
  • 3/ the value is returned — 2

This execution flow is slight the same for a call to computer[:cores] = 2 and computer.cores.

Voilà !

May I have your attention please 🎤🎤

Feel free to subscribe here:

Thank you for taking the time to read this post :-)

Feel free to 👏 and share this article if it has been useful for you. 🚀

Here is a link to my last article: Struct in Ruby.


E-Learning platform for Ruby and Ruby on Rails

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store