Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

Struct.new

Struct.new(*member_names, keyword_init: false){|Struct_subclass| ... } → Struct_subclass
Struct.new(class_name, *member_names, keyword_init: false){|Struct_subclass| ... } → Struct_subclass
Struct_subclass.new(*member_names) → Struct_subclass_instance
Struct_subclass.new(**member_names) → Struct_subclass_instance

Struct.new returns a new subclass of Struct. The new subclass:

  • May be anonymous, or may have the name given by class_name.

  • May have members as given by member_names.

  • May have initialization via ordinary arguments (the default) or via keyword arguments (if keyword_init: true is given).

The new subclass has its own method ::new; thus:

Example
Foo = Struct.new('Foo', :foo, :bar) # => Struct::Foo
f = Foo.new(0, 1)                   # => #<struct Struct::Foo foo=0, bar=1>

Class Name

With string argument class_name, returns a new subclass of Struct named Struct::<em>class_name</em>:

Example
Foo = Struct.new('Foo', :foo, :bar) # => Struct::Foo
Foo.name                            # => "Struct::Foo"
Foo.superclass                      # => Struct

Without string argument class_name, returns a new anonymous subclass of Struct:

Example
Struct.new(:foo, :bar).name # => nil

Block

With a block given, the created subclass is yielded to the block:

Example
Customer = Struct.new('Customer', :name, :address) do |new_class|
  p "The new subclass is #{new_class}"
  def greeting
    "Hello #{name} at #{address}"
  end
end           # => Struct::Customer
dave = Customer.new('Dave', '123 Main')
dave # =>     #<struct Struct::Customer name="Dave", address="123 Main">
dave.greeting # => "Hello Dave at 123 Main"

Output, from Struct.new:

Example
"The new subclass is Struct::Customer"

Member Names

Symbol arguments member_names determines the members of the new subclass:

Example
Struct.new(:foo, :bar).members        # => [:foo, :bar]
Struct.new('Foo', :foo, :bar).members # => [:foo, :bar]

The new subclass has instance methods corresponding to member_names:

Example
Foo = Struct.new('Foo', :foo, :bar)
Foo.instance_methods(false) # => [:foo, :bar, :foo=, :bar=]
f = Foo.new                 # => #<struct Struct::Foo foo=nil, bar=nil>
f.foo                       # => nil
f.foo = 0                   # => 0
f.bar                       # => nil
f.bar = 1                   # => 1
f                           # => #<struct Struct::Foo foo=0, bar=1>

Singleton Methods

A subclass returned by Struct.new has these singleton methods:

  • Method ::new creates an instance of the subclass:

    Example
    Foo.new          # => #<struct Struct::Foo foo=nil, bar=nil>
    Foo.new(0)       # => #<struct Struct::Foo foo=0, bar=nil>
    Foo.new(0, 1)    # => #<struct Struct::Foo foo=0, bar=1>
    Foo.new(0, 1, 2) # Raises ArgumentError: struct size differs
    

    Method ::[] is an alias for method ::new.

  • Method :inspect returns a string representation of the subclass:

    Example
    Foo.inspect
    # => "Struct::Foo"
    
  • Method ::members returns an array of the member names:

    Example
    Foo.members # => [:foo, :bar]
    

Keyword Argument

By default, the arguments for initializing an instance of the new subclass are ordinary arguments (not keyword arguments). With optional keyword argument keyword_init: true, the new subclass is initialized with keyword arguments:

Example
# Without keyword_init: true.
Foo = Struct.new('Foo', :foo, :bar)
Foo                     # => Struct::Foo
Foo.new(0, 1)           # => #<struct Struct::Foo foo=0, bar=1>
# With keyword_init: true.
Bar = Struct.new(:foo, :bar, keyword_init: true)
Bar # =>                # => Bar(keyword_init: true)
Bar.new(bar: 1, foo: 0) # => #<struct Bar foo=0, bar=1>
See also .members #== .keyword_init? #[] also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0