Class: MethodPattern::PatternMatchedFunction
- Inherits:
-
Object
- Object
- MethodPattern::PatternMatchedFunction
- Defined in:
- lib/method_pattern.rb
Defined Under Namespace
Classes: Pattern
Instance Method Summary collapse
-
#initialize(name) ⇒ PatternMatchedFunction
constructor
A new instance of PatternMatchedFunction.
- #match(args) ⇒ Object
- #with(*patterns, &block) ⇒ Object
Constructor Details
#initialize(name) ⇒ PatternMatchedFunction
16 17 18 19 20 21 22 23 |
# File 'lib/method_pattern.rb', line 16 def initialize name @name = name @patterns = [] @default = proc do |*args| raise ArgumentError, "method #{self.class.inspect}##{name} does not know how to handle arguments: #{args.map(&:inspect).join(', ')}" end end |
Instance Method Details
#match(args) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/method_pattern.rb', line 29 def match args @patterns.each do |pattern| if pattern.match? args return pattern.block end end @default end |
#with(*patterns, &block) ⇒ Object
25 26 27 |
# File 'lib/method_pattern.rb', line 25 def with *patterns, &block @patterns << Pattern.new(patterns, block) end |