Class: Ludy::PatternMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ludy/pattern_matcher.rb

Overview

someone who does the pattern matching thing see Kernel#defun

Instance Method Summary collapse

Constructor Details

#initialize(args, &fun) ⇒ PatternMatcher

init for the first parameter



10
# File 'lib/ludy/pattern_matcher.rb', line 10

def initialize args, &fun; @params = [[args, fun]]; end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(msg, *args, &block) ⇒ Object

delegate all the rest message to @params array



20
21
22
23
24
25
26
# File 'lib/ludy/pattern_matcher.rb', line 20

def method_missing msg, *args, &block
  if @params.respond_to? msg
    @params.public_send msg, *args, &block
  else
    raise NoMethodError.new("PatternMatcher doesn't respond to #{msg}")
  end
end

Instance Method Details

#first_match(args) ⇒ Object

find the first match of this arguments. notice, this is not the best match. maybe someday i could implement the match policy accordingly or selectable policy.



14
15
16
17
18
# File 'lib/ludy/pattern_matcher.rb', line 14

def first_match args
  @params.find{ |parameters, fun|
    match? parameters, args
  }.ergo.last
end