Module: Deterministic::PatternMatching::Match
- Defined in:
- lib/deterministic/match.rb
Defined Under Namespace
Classes: Matcher
Instance Method Summary collapse
-
#any(value = nil, &result_block) ⇒ Object
catch-all.
- #call ⇒ Object
- #initialize(container, context) ⇒ Object
Instance Method Details
#any(value = nil, &result_block) ⇒ Object
catch-all
29 30 31 |
# File 'lib/deterministic/match.rb', line 29 def any(value=nil, &result_block) push(Object, value, result_block) end |
#call ⇒ Object
21 22 23 24 25 26 |
# File 'lib/deterministic/match.rb', line 21 def call value = @container.respond_to?(:value) ? @container.value : nil matcher = @collection.detect { |m| m.matches?(value) } raise NoMatchError, "No match could be made for #{@container.inspect}" if matcher.nil? @context.instance_exec(value, &matcher.block) end |
#initialize(container, context) ⇒ Object
15 16 17 18 19 |
# File 'lib/deterministic/match.rb', line 15 def initialize(container, context) @container = container @context = context @collection = [] end |