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
More blocks, classes and pattern matching commands
- Write a method that takes a block
- Define a class
- Share behavior with a module
- Match patterns
- Create a lambda
All blocks, classes and pattern matching commands · Ruby 3 cheatsheet