Ruby 3 · Blocks, classes and pattern matching

Share behavior with a module

Include a module to add its methods to a class. Including Comparable gives you < > == from <=>.

class Version
  include Comparable
  attr_reader :number

  def initialize(number) = @number = number
  def <=>(other) = number <=> other.number
end

puts Version.new(2) > Version.new(1)

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?