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