Structs are functionally the same as classes, but have lots of default functionality built in to save time. They’re useful if you need objects for storing and managing variables, without the full range of class features.

Structs can be used to just store parameters, but can also use methods with these parameters too.

ExampleStruct =, :param2)

ExampleStruct2 =, :integer2) do
def sum
integer1 + integer2

struct_1 ="string1", "string2")
struct_2 =, 2)

struct_1.param1 # "string1"
struct_2.sum # 3

Structs are also great for organizing data within classes themselves. This is my version of the example from this Struct explaination, using a struct to organize address info in a class for people. Notice the syntax for using variables and methods from the class itself and the struct inside it. This is a simple and more accessible syntax than hashes, so structs are useful for this reason too.

class Person
Address =, :street_2, :city, :province, :country, :postal_code) do
def full_address
"#{street_1} #{city} #{country} #{postal_code}"

attr_accessor :name, :address

def initialize(name, opts)
@name = name
@address =[:street_1], opts[:street_2], opts[:city], opts[:province], opts[:country], opts[:postal_code])

def about
"#{@name}\'s address is #{@address.full_address}"

leigh ="Leigh Halliday", {
street_1: "123 Road",
city: "Toronto",
province: "Ontario",
country: "Canada",
postal_code: "M5E 0A3"

puts # Leigh Halliday
puts # Toronto
puts leigh.about # Leigh Halliday's address is 123 Road Toronto Canada M5E 0A3