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
28 29 30 |
# File 'lib/deterministic/match.rb', line 28 def any(value=nil, &result_block) push(Object, value, result_block) end |
#call ⇒ Object
20 21 22 23 24 25 |
# File 'lib/deterministic/match.rb', line 20 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
14 15 16 17 18 |
# File 'lib/deterministic/match.rb', line 14 def initialize(container, context) @container = container @context = context @collection = [] end |