Module: Algebrick::Matching
Overview
include this module anywhere yoy need to use pattern matching
Class Method Summary (collapse)
Instance Method Summary (collapse)
Class Method Details
+ (Object) match_value(matcher, block) (private)
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/algebrick/matching.rb', line 47 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 |
Instance Method Details
- (Object) any
18 19 20 |
# File 'lib/algebrick/matching.rb', line 18 def any Matchers::Any.new end |
- (Object) match(value, *cases)
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/algebrick/matching.rb', line 22 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 Matching.match_value matcher, block if matcher === value end raise "no match for (#{value.class}) '#{value}' by any of #{cases.map(&:first).join ', '}" end |
- (Object) on(matcher, value = nil, &block)
35 36 37 38 39 40 41 42 43 |
# File 'lib/algebrick/matching.rb', line 35 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 |