Class: Factbase::Nth

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

Overview

Retrieves the value of a specified key from the nth map.

Instance Method Summary collapse

Methods included from TermShared

#to_s

Constructor Details

#initialize(operands) ⇒ Nth

Constructor.

Parameters:

  • operands (Array)

    Operands



12
13
14
15
16
# File 'lib/factbase/terms/nth.rb', line 12

def initialize(operands)
  super()
  @operands = operands
  @op = :nth
end

Instance Method Details

#evaluate(_fact, maps, _fb) ⇒ Object

Evaluate term on a fact.

Parameters:

Returns:

  • (Object)

    The value of the specified key from the nth map



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

def evaluate(_fact, maps, _fb)
  assert_args(2)
  pos = @operands[0]
  raise "An integer is expected, but #{pos} provided" unless pos.is_a?(Integer)
  k = @operands[1]
  raise "A symbol is expected, but #{k} provided" unless k.is_a?(Symbol)
  m = maps[pos]
  return nil if m.nil?
  m[k.to_s]
end