Ruby 3 · Blocks, classes and pattern matching

Define simple data classes

Data.define creates immutable value objects (Ruby 3.2+); Struct is the mutable version.

Point = Data.define(:x, :y)
p1 = Point.new(x: 1, y: 2)
p2 = p1.with(y: 5)
puts p1, p2

Official Ruby 3 documentation

More blocks, classes and pattern matching commands

All blocks, classes and pattern matching commands · Ruby 3 cheatsheet

Was this page helpful?