
Security News
CVE Volume Surges Past 48,000 in 2025 as WordPress Plugin Ecosystem Drives Growth
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.
immutable-struct
Advanced tools
= ImmutableStruct
{}[https://travis-ci.org/stitchfix/immutable-struct]
Creates struct-like classes (that can build value objects) that do not have setters and also have better constructors than Ruby's built-in +Struct+.
This is highly useful for creating presenters, non-database-related models, or other quick and dirty classes in your application. Instead of using a +Hash+ or +OpenStruct+, you can create a bit more clarity around your types by using +ImmutableStruct+, which is almost as convienient.
== Install
Add to your +Gemfile+:
gem 'immutable-struct'
Then install:
bundle install
If not using bundler, just use RubyGems:
gem install immutable-struct
== To use
Person = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses]) do
def minor?
age < 18
end
end
p = Person.new(name: "Dave", # name will be 'Dave'
age: 40, # age will be 40
# job is omitted, so will be nil
active: true) # active and active? will be true
# addresses is omitted, but since we've selected
# Array coercion, it'll be []
p.name # => "Dave"
p.age # => 40
p.active? # => true
p.minor? # => false
p.addresses # => []
p2 = Person.new(name: "Dave", age: 40, active: true)
p == p2 # => true
p.eql?(p2) # => true
SimilarPerson = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses])
sp = SimilarPerson.new(name: "Dave", age: 40, active: true)
p == sp # => false # Different class leads to inequality
new_person = p.merge(name: "Other Dave", age: 41) # returns a new object with merged attributes
new_person.name # => "Other Dave"
new_person.age # => 41
new_person.active? # => true
You can coerce values into struct types by using the +from+ method. This is similar to Ruby's conversion functions, e.g. Integer("1").
dave = Person.from(p)
dave.equal?(p) # => true (object equality)
daveish = Person.from(dave.to_h)
daveish.equal?(dave) # => false
daveish == dave # => true
You can treat the interior of the block as a normal class definition with the exception of setting constants. Use +const_set+ to scope constants as-expected.
Point = ImmutableStruct.new(:x, :y) do
const_set(:ZERO, 0)
ONE_HUNDRED = 100
end
Point::ZERO # => 0
::ONE_HUNDRED # => 100
::ZERO # => NameError: uninitialized constant ZERO
== Links
FAQs
Unknown package
We found that immutable-struct demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
CVE disclosures hit a record 48,185 in 2025, driven largely by vulnerabilities in third-party WordPress plugins.

Security News
Socket CEO Feross Aboukhadijeh joins Insecure Agents to discuss CVE remediation and why supply chain attacks require a different security approach.

Security News
Tailwind Labs laid off 75% of its engineering team after revenue dropped 80%, as LLMs redirect traffic away from documentation where developers discover paid products.