It’s unfortunate that the names are so similar, but let’s tear these two apart.


We’ll start with Struct. Ruby’s official documentation bills Struct as:

a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.”

Struct is essentially a shortcut for building simple classes with attributes and maybe a method or two. Let’s see how it’s used:

User =, :last) do
  def email

mike ="Michael", "Rodrigues")

#=> "Michael"

#=> "Rodrigues"
#=> ""

If the example looks familiar, I basically ripped it right out of the Ruby core documentation. Make a mental note of that; Struct is part of Ruby core. Its source code is written in C. This is in contrast to OpenStruct which is part of the standard library. It’s written in Ruby and you have to require it before you use it. You don’t need to do anything to use Struct.

Now, back to Struct. It might just be easier to show you a class that gives you what Struct gives you with much less work:

class User

  attr_accessor :first
  attr_accessor :last

  def initialize(first, last)
    @first = first
    @last = last

  def email


It really returns a class, keep this in mind as we transition our focus to OpenStruct.


mike ="Michael", "Rodrigues")
#=> User

The official Ruby documentation describes OpenStruct as:

a data structure, similar to a Hash, that allows the definition of arbitrary attributes with their accompanying values. This is accomplished by using Ruby’s metaprogramming to define methods on the class itself.

So unlike Struct, OpenStruct isn’t going to define a new class to instantiate objects from. However, it will let you arbitrarily define new attributes on the fly. Let’s look a tweaked example from the Ruby documentation:

require "ostruct" # remember, part of std lib, not core

event =
event.timestamp =
event.message = "Something notable happened."

#=> 2019-08-29 23:20:29 -0700

#=> "Something Notable happened."

#=> nil

event.sender = "Michael"
#=> "Michael"

You can also pass in a Hash at instantiation:

require "ostruct"

event =
			  message: "Short message.",
			  sender: "Michael"

#=> "Michael"


So, in summary:

  • returns a class with fixed attributes
  • returns an instance of OpenStruct, attributes can be added and removed after instantiation

  • Struct is part of Ruby core, written in C
  • OpenStruct is part of the Ruby Standard Library, written in Ruby

  • Struct is instantiated with a class name, list of attributes, and optional methods
  • OpenStruct can be instantiated with a Hash

Use Struct when speed is a concern, and you know all of your attributes at instantiation.

Use OpenStruct if you’re OK to sacrifice some speed for the flexibility of defining attributes after instantiation. You can use a Hash but you won’t get the “syntactical sugar” that OpenStruct gives you with the dynamically created setter and getter methods.