Create Named Structs With Struct.new
I often see Struct
used to create some one-off anonymous data structure
like so:
> person = Struct.new(:name, :age)
=> #
> person.new("Alice", 33)
=> #
This will often get the job done, but on its own the resulting data structure doesn’t tell us as much as it could.
We can say more with a named struct:
Struct.new("Person", :name, :age)
=> Struct::Person
> Struct::Person.new("Bob", 24)
=> #
When the first argument is a string that can be converted to a constant,
then we’ll get a named struct that is subclassed under Struct
.
We can also assign the struct initialization to a constant to do a similar thing:
> Person = Struct.new(:name, :age)
=> Person
> Person.new("Jerry", 45)
=> #