Module: Factbase::Strings

Included in:
Term
Defined in:
lib/factbase/terms/strings.rb

Overview

String terms.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Instance Method Details

#concat(fact, maps, fb) ⇒ Object



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

def concat(fact, maps, fb)
  (0..(@operands.length - 1)).map { |i| _values(i, fact, maps, fb)&.first }.join
end

#matches(fact, maps, fb) ⇒ Object



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

def matches(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

#sprintf(fact, maps, fb) ⇒ Object



18
19
20
21
22
# File 'lib/factbase/terms/strings.rb', line 18

def sprintf(fact, maps, fb)
  fmt = _values(0, fact, maps, fb)[0]
  ops = (1..(@operands.length - 1)).map { |i| _values(i, fact, maps, fb)&.first }
  format(*([fmt] + ops))
end