Ruby Struct Explained

Ruby Struct Explained

Simplifying Data Structures

In simple words, Ruby Struct is a built-in class that provides useful functionalities and shortcuts. You can use it for both logic and tests. I will quickly go through its features, compare with other similar stuff, and show some less-known but still useful information about it.

All my notes are based on years of hands-on experience at iRonin.IT - a top software development company, where we provide custom software development and IT staff augmentation services for a wide array of technologies.

Basic usage

Employee =, :last_name)
employee ="John", "Doe")
employee.first_name # => "John"
employee.last_name # => "Doe"

As you can see, it behaves like a simple Ruby class. The above code is equivalent to:

class Employee
  attr_reader :first_name, :last_name

  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name

employee ="John", "Doe")

What if we want to define the #full_name name on our Employee class? We can do it with Struct as well:

Employee =, :last_name) do
  def full_name
    "#{first_name} #{last_name}"

employee ="John", "Doe")
employee.full_name # => "John Doe"

When to use Struct

Struct is often used to make code cleaner, format more structured data from a hash, or as a replacement for real-world classes in tests.

  1. Temporary data structure - the most popular example is a geocoding response where you want to form Address object with attributes instead of a hash with the geocoded data.

  2. Cleaner code

  3. Testing - as long as Struct responds to the same methods as the object used in tests, you can replace it if it does make sense. You can consider using it when testing dependency injection.

When not to use Struct

Avoid inheritance from Struct when you can. I intentionally assigned Struct to constant in the above example instead of doing this:

class Employee <, :last_name)
  def full_name
    "#{first_name} #{last_name}"

When your class inherits from Struct you may not realize that:

  1. Arguments are not required - if one of the passed arguments it's an object then calling a method on it will cause an error

  2. Attributes are always public - it is far from perfect encapsulation unless you desire such behavior

  3. Instances are equal if their attributes are equal - ==

Play with it

Access the class attributes the way you want:

person ="John")
person.first_name # => "John"
person[:first_name] # => "John"
person["first_name"] # => "John"

Use the equality operator:

Person ="John") =="John") # => true

Iterate over values or pairs:

Person =, :last_name)
person ="John", "Doe")
# Values

person.each do |value|
  puts value
# >> "John"
# >> "Doe"

# Pairs

person.each_pair do |key, value|
  puts "#{key}: #{value}"
# >> "first_name: John"
# >> "last_name: Doe"


Address =
Person =, :address)
address ="New York")
person ="John Doe", address)

person.dig(:address, :city) # => "New York"



Hash is also considered as an alternative to Struct. It is faster to use but has worse performance than its opponent (I will test it a little bit later in this article).


OpenStruct is slower but more flexible alternative. Using it you can assign attribute dynamically and it does not require predefined attributes. All you have to do is to pass a hash with attributes:

require 'ostruct'

employee = "John", last_name: "Doe")
employee.first_name # => "John"
employee.age = 30
employee.age # => 30

Standard boilerplate

Although it may get annoying when you have to type it multiple times:

class Employee
  def initialize(first_name, last_name)
    @first_name = first_name
    @last_name = last_name

  def full_name
    "#{first_name} #{last_name}"

Alternatives comparison

NameNon existing attributesDynamically add attributePerformance (lower is better)
Structraises errorno2
Open Structreturns nilyes3
Hashreturns nilyes1


I used the following code to measure the performance of the above solutions: 10 do |bench| "Hash: " do
    10_000_000.times do { name: "John Doe", city: "New York" } end
  end "Struct: " do
    klass =, :age)
    10_000_000.times do"John Doe", "New York") end
  end "Open Struct: " do
    10_000_000.times do "John Doe", city: "New York") end


Open Struct127.2530800.540836127.793916127.904832