module Algebrick::Matching
include this module anywhere yoy need to use pattern matching
Private Class Methods
match_value(matcher, block)
click to toggle source
# File lib/algebrick/matching.rb, line 53 def self.match_value(matcher, block) if block.kind_of? Proc if matcher.kind_of? Matchers::Abstract matcher.assigns &block else block.call end else block end end
Public Instance Methods
any()
click to toggle source
# File lib/algebrick/matching.rb, line 18 def any Matchers::Any.new end
match(value, *cases)
click to toggle source
# File lib/algebrick/matching.rb, line 22 def match(value, *cases) success, result = match? value, *cases raise "no match for (#{value.class}) '#{value}' by any of #{cases.map(&:first).join ', '}" unless success result end
match?(value, *cases)
click to toggle source
# File lib/algebrick/matching.rb, line 28 def match?(value, *cases) cases = if cases.size == 1 && cases.first.is_a?(Hash) cases.first else cases end cases.each do |matcher, block| return true, Matching.match_value(matcher, block) if matcher === value end [false, nil] end
on(matcher, value = nil, &block)
click to toggle source
# File lib/algebrick/matching.rb, line 41 def on(matcher, value = nil, &block) matcher = if matcher.is_a? Matchers::Abstract matcher else matcher.to_m end raise ArgumentError, 'only one of block or value can be supplied' if block && value [matcher, value || block] end