Class: PatternMatch::PatternCondition

Inherits:
PatternElement show all
Defined in:
lib/pattern-match/core.rb

Instance Attribute Summary

Attributes inherited from Pattern

#next, #parent, #prev

Instance Method Summary collapse

Methods inherited from PatternElement

#quantifier?

Methods inherited from Pattern

#!@, #&, #ancestors, #append, #quantified?, #quantifier?, #quasibinding, #root, #root?, #to_a, #vars, #|

Constructor Details

#initialize(&condition) ⇒ PatternCondition

Returns a new instance of PatternCondition.



497
498
499
500
# File 'lib/pattern-match/core.rb', line 497

def initialize(&condition)
  super()
  @condition = condition
end

Instance Method Details

#inspectObject



516
517
518
# File 'lib/pattern-match/core.rb', line 516

def inspect
  "#<#{self.class.name}: condition=#{@condition.inspect}>"
end

#match(vals) ⇒ Object



502
503
504
505
506
507
508
509
# File 'lib/pattern-match/core.rb', line 502

def match(vals)
  return false unless vals.empty?
  if @condition.call
    @next ? @next.match(vals) : true
  else
    false
  end
end

#validateObject



511
512
513
514
# File 'lib/pattern-match/core.rb', line 511

def validate
  super
  raise MalformedPatternError if ancestors.find {|i| i.next and ! i.next.kind_of?(PatternCondition) }
end