How to create value objects in Ruby – the idiomatic way
Published on: 2025-05-30 09:43:16
When writing Ruby OOP, a typical pattern might be to create an object to group multiple values together meaningfully and sometimes also add some extra methods (computed properties, predicates, representations, …) to allow the object to respond to various situations.
Here is an exploration of how to create value-alike objects in Ruby and what I think is the modern idiomatic way.
What is a value-alike object?
If you want to read an article about this concept, I recommend Value Object by Martin Fowler. He explains this concept very well with examples and references. I invite you to read that article. It is not that long.
They are simple objects that have the following properties:
Comparable by type and value (that means two objects having the same values as attributes and being the same class will be equal)
Immutable (once set their attributes, they should not be allowed to be changed)
The concept is useful when you have to carry around multiple related values, and you need them to
... Read full article.