Skip to content
On this page

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

class · Ruby 3.3

Struct

< Object include Enumerable 23 instance methods 4 class methods

Class Struct provides a convenient way to create a simple class that can store and fetch values.

This example creates a subclass of Struct, Struct::Customer; the first argument, a string, is the name of the subclass; the other arguments, symbols, determine the members of the new subclass.

Example
Customer = Struct.new('Customer', :name, :address, :zip)
Customer.name       # => "Struct::Customer"
Customer.class      # => Class
Customer.superclass # => Struct

Corresponding to each member are two methods, a writer and a reader, that store and fetch values:

Example
methods = Customer.instance_methods false
methods # => [:zip, :address=, :zip=, :address, :name, :name=]

An instance of the subclass may be created, and its members assigned values, via method ::new:

Example
joe = Customer.new("Joe Smith", "123 Maple, Anytown NC", 12345)
joe # => #<struct Struct::Customer name="Joe Smith", address="123 Maple, Anytown NC", zip=12345>

The member values may be managed thus:

Example
joe.name    # => "Joe Smith"
joe.name = 'Joseph Smith'
joe.name    # => "Joseph Smith"

And thus; note that member name may be expressed as either a string or a symbol:

Example
joe[:name]  # => "Joseph Smith"
joe[:name] = 'Joseph Smith, Jr.'
joe['name'] # => "Joseph Smith, Jr."

See Struct::new.

What’s Here

First, what’s elsewhere. Class Struct:

See also Data, which is a somewhat similar, but stricter concept for defining immutable value objects.

Here, class Struct provides methods that are useful for:

Methods for Creating a Struct Subclass

  • ::new: Returns a new subclass of Struct.

Methods for Querying

  • hash: Returns the integer hash code.

  • length, size: Returns the number of members.

Methods for Comparing

  • ==: Returns whether a given object is equal to self, using == to compare member values.

  • eql?: Returns whether a given object is equal to self, using eql? to compare member values.

Methods for Fetching

  • []: Returns the value associated with a given member name.

  • to_a, values, deconstruct: Returns the member values in self as an array.

  • deconstruct_keys: Returns a hash of the name/value pairs for given member names.

  • dig: Returns the object in nested objects that is specified by a given member name and additional arguments.

  • members: Returns an array of the member names.

  • select, filter: Returns an array of member values from self, as selected by the given block.

  • values_at: Returns an array containing values for given member names.

Methods for Assigning

  • []=: Assigns a given value to a given member name.

Methods for Iterating

  • each: Calls a given block with each member name.

  • each_pair: Calls a given block with each member name/value pair.

Methods for Converting

  • inspect, to_s: Returns a string representation of self.

  • to_h: Returns a hash of the member name/value pairs in self.

Class methods 4

Instance methods 23

Ruby version changes 3

  1. 3.2
    • A Struct class can also be initialized with keyword arguments without keyword_init: true on Struct.new [Feature #16806]

      Example
      Post = Struct.new(:id, :name)
      Post.new(1, "hello") #=> #<struct Post id=1, name="hello">
      # From Ruby 3.2, the following code also works without keyword_init: true.
      Post.new(id: 1, name: "hello") #=> #<struct Post id=1, name="hello">
      
  2. 3.1
    • Passing only keyword arguments to Struct#initialize is warned. You need to use a Hash literal to set a Hash to a first member. [Feature #16806]

    • StructClass#keyword_init? is added [Feature #18008]