Class: Factbase::First

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

Overview

The ‘first’ term is used to retrieve the value of a specified key from the first map in a set of maps.

Instance Method Summary collapse

Methods included from TermShared

#to_s

Constructor Details

#initialize(operands) ⇒ First

Constructor.

Parameters:

  • operands (Array)

    Operands



11
12
13
14
15
# File 'lib/factbase/terms/first.rb', line 11

def initialize(operands)
  super()
  @operands = operands
  @op = :first
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 first map



22
23
24
25
26
27
28
29
# File 'lib/factbase/terms/first.rb', line 22

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