Struct là shorthand tạo class đơn giản với accessor cho từng field đã khai báo — tự động có initialize, getter/setter, ==, to_s, members.
Point = Struct.new(:x, :y)
p = Point.new(3, 4)
p.x # => 3
p == Point.new(3, 4) # => true (value equality)
p.to_s # => "#<struct Point x=3, y=4>"Struct còn nhận block để thêm custom method. Khi dùng Struct: value object đơn giản (toạ độ, khoảng thời gian, kết quả query), data transfer object không cần behavior phức tạp. Khi dùng class: kế thừa, logic phức tạp, cần kiểm soát visibility.
Ruby 3.2+ có Data.define cho immutable value object.
Struct is a shorthand for creating a simple class with accessors for declared fields — automatically provides initialize, getters/setters, ==, to_s, and members.
Point = Struct.new(:x, :y)
p = Point.new(3, 4)
p.x # => 3
p == Point.new(3, 4) # => true (value equality)
p.to_s # => "#<struct Point x=3, y=4>"Struct also accepts a block to add custom methods. Use Struct for: simple value objects (coordinates, time ranges, query results), data-transfer objects without complex behaviour. Use a class when: inheritance is needed, logic is complex, or visibility control is required.
Ruby 3.2+ adds Data.define for immutable value objects.