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