class Pattern

@brief pattern matching for Ruby

Attributes

cases[RW]

Public Class Methods

new(cases) click to toggle source
# File lib/Olib/pattern.rb, line 7
def initialize(cases)
  @cases = cases
end

Public Instance Methods

match(str) click to toggle source
# File lib/Olib/pattern.rb, line 11
def match(str)
  found = @cases.each_pair.find do |exp, handler|
    str =~ exp
  end

  if !found
    raise Exception.new [
      "Error: inexhaustive pattern",
      "counterexample: #{str}",
    ].join("\n")
  end

  exp, handler = found

  handler.call exp.match(str).to_struct
end
to_proc() click to toggle source
# File lib/Olib/pattern.rb, line 28
def to_proc
  patt = self
  Proc.new do |str|
    patt.match str
  end
end