Class: Factbase::Matches

Inherits:
TermBase show all
Defined in:
lib/factbase/terms/matches.rb

Overview

Matches term.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Methods included from TermShared

#to_s

Constructor Details

#initialize(operands) ⇒ Matches

Returns a new instance of Matches.



14
15
16
17
# File 'lib/factbase/terms/matches.rb', line 14

def initialize(operands)
  super()
  @operands = operands
end

Instance Method Details

#evaluate(fact, maps, fb) ⇒ Boolean

Evaluate term on a fact.

Parameters:

Returns:

  • (Boolean)

    True if the string matches the regexp, false otherwise



24
25
26
27
28
29
30
31
32
33
# File 'lib/factbase/terms/matches.rb', line 24

def evaluate(fact, maps, fb)
  assert_args(2)
  str = _values(0, fact, maps, fb)
  return false if str.nil?
  raise 'Exactly one string is expected' unless str.size == 1
  re = _values(1, fact, maps, fb)
  raise 'Regexp is nil' if re.nil?
  raise 'Exactly one regexp is expected' unless re.size == 1
  str[0].to_s.match?(re[0])
end