Class: Factbase::Tuples

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/tuples.rb

Overview

With the help of this class, it’s possible to select a few facts from a factbase at a time, which depend on each other. For example, it’s necessary to find a fact where the name is set and then find another fact, where the salary is the salary is the same as in the first found fact. Here is how:

Factbase::Tuples.new(qt, ['(exists name)', '(eq salary, {f0.salary})']).each do |a, b|
  puts a.name
  puts b.salary
end

Here, the {f0.salary} is a special substitution place, which is replaced by the salary of the fact that is found by the previous query.

The indexing of queries starts from zero.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(fb, queries) ⇒ Tuples

Returns a new instance of Tuples.



45
46
47
48
# File 'lib/factbase/tuples.rb', line 45

def initialize(fb, queries)
  @fb = fb
  @queries = queries
end

Instance Method Details

#each {|Array<Fact>| ... } ⇒ Integer

Iterate them one by one.

Yields:

  • (Array<Fact>)

    Arrays of facts one-by-one

Returns:

  • (Integer)

    Total number of arrays yielded



53
54
55
56
# File 'lib/factbase/tuples.rb', line 53

def each(&)
  return to_enum(__method__) unless block_given?
  each_rec([], @queries, &)
end