Sha256: ef6e73c7c1b23755975ac1b304521b9624277c940c07d36ad59c12ffd4fc7c7e

Contents?: true

Size: 1.15 KB

Versions: 14

Compression:

Stored size: 1.15 KB

Contents

require 'ludy/kernel/public_send'

module Ludy
  # someone who does the pattern matching thing
  # see Kernel#defun
  class PatternMatcher
    # init for the first parameter
    def initialize args, &fun; @params = [[args, fun]]; end
    # 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.
    def first_match args
      @params.find{ |parameters, fun|
        match? parameters, args
      }.ergo.last
    end
    # delegate all the rest message to @params array
    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

    private
    def match? parameters, arguments
      return false unless parameters.size == arguments.size
      parameters.zip(arguments).each{ |param, arg|
        if param.kind_of? Class
          return false unless arg.kind_of?(param)
        else
          return false unless arg == param
        end
      }
      return true
    end
  end

end # of Ludy

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
godfat-ludy-0.1.13 lib/ludy/pattern_matcher.rb
ludy-0.1.1 lib/ludy/pattern_matcher.rb
ludy-0.1.10 lib/ludy/pattern_matcher.rb
ludy-0.1.0 lib/ludy/pattern_matcher.rb
ludy-0.1.13 lib/ludy/pattern_matcher.rb
ludy-0.1.2 lib/ludy/pattern_matcher.rb
ludy-0.1.11 lib/ludy/pattern_matcher.rb
ludy-0.1.3 lib/ludy/pattern_matcher.rb
ludy-0.1.6 lib/ludy/pattern_matcher.rb
ludy-0.1.4 lib/ludy/pattern_matcher.rb
ludy-0.1.5 lib/ludy/pattern_matcher.rb
ludy-0.1.7 lib/ludy/pattern_matcher.rb
ludy-0.1.8 lib/ludy/pattern_matcher.rb
ludy-0.1.9 lib/ludy/pattern_matcher.rb