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.



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

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

Instance Method Details

#inspectObject



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

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

#match(vals) ⇒ Object



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

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

#validateObject



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

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