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, #directly_quantified?, #quantified?, #quantifier?, #quasibinding, #root, #root?, #to_a, #vars, #|

Methods included from PatternMatch::Pattern::Backtrackable

#choice_points

Constructor Details

#initialize(&condition) ⇒ PatternCondition

Returns a new instance of PatternCondition.



452
453
454
455
# File 'lib/pattern-match/core.rb', line 452

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

Instance Method Details

#inspectObject



471
472
473
# File 'lib/pattern-match/core.rb', line 471

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

#match(vals) ⇒ Object



457
458
459
460
461
462
463
464
# File 'lib/pattern-match/core.rb', line 457

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

#validateObject



466
467
468
469
# File 'lib/pattern-match/core.rb', line 466

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