Skip to content

End of Life Ruby 3.2 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, or via keyword arguments

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
    
    # Initialization with keyword arguments:
    Foo.new(foo: 0)         # => #<struct Struct::Foo foo=0, bar=nil>
    Foo.new(foo: 0, bar: 1) # => #<struct Struct::Foo foo=0, bar=1>
    Foo.new(foo: 0, bar: 1, baz: 2)
    # Raises ArgumentError: unknown keywords: baz
    

    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 can be both positional and keyword arguments.

Optional keyword argument keyword_init: allows to force only one type of arguments to be accepted:

Example
KeywordsOnly = Struct.new(:foo, :bar, keyword_init: true)
KeywordsOnly.new(bar: 1, foo: 0)
# => #<struct KeywordsOnly foo=0, bar=1>
KeywordsOnly.new(0, 1)
# Raises ArgumentError: wrong number of arguments

PositionalOnly = Struct.new(:foo, :bar, keyword_init: false)
PositionalOnly.new(0, 1)
# => #<struct PositionalOnly foo=0, bar=1>
PositionalOnly.new(bar: 1, foo: 0)
# => #<struct PositionalOnly foo={:foo=>1, :bar=>2}, bar=nil>
# Note that no error is raised, but arguments treated as one hash value

# Same as not providing keyword_init:
Any = Struct.new(:foo, :bar, keyword_init: nil)
Any.new(foo: 1, bar: 2)
# => #<struct Any foo=1, bar=2>
Any.new(1, 2)
# => #<struct Any foo=1, bar=2>

Ruby version changes

  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">
      
See also .members #== .keyword_init? #[] also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0