Hacker News new | ask | show | jobs
by glennericksen 445 days ago
I don't usually think of Data when grouping values together in Ruby. Seems like I should. Lucian puts forth a good explainer of when and why they are helpful.

To summarize, Data became available a few years ago in Ruby 3.2. You can create value objects by subclassing the Data class with Data.define. Data objects are immutable, comparable and easily greppable. They are constrained in ways Struct, Hash and Class are not. The shorthand removes boilerplate and the constraints create the utility.

Measure = Data.define(:amount, :unit) weight = Measure.new(amount: 50, unit: 'kg')