Class: OMF::Rete::Tuple

Inherits:
Object
  • Object
show all
Defined in:
lib/omf_rete/tuple.rb

Overview

This class represents a tuple and includes various ways to access the contained elements..

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tarray, description) ⇒ Tuple

Returns a new instance of Tuple.



43
44
45
46
# File 'lib/omf_rete/tuple.rb', line 43

def initialize(tarray, description)
  @tarray = tarray
  @description = description
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



48
49
50
# File 'lib/omf_rete/tuple.rb', line 48

def method_missing(name, *args, &block)
  self[name]
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/omf_rete/tuple.rb', line 8

def description
  @description
end

Instance Method Details

#[](index_or_name) ⇒ Object

Return a specific element either indicated by index (number) or name as listed in ‘description’.



32
33
34
35
36
37
38
39
40
41
# File 'lib/omf_rete/tuple.rb', line 32

def [](index_or_name)
  if index_or_name.is_a? Integer
    return @tarray[index_or_number]
  elsif index_or_name.is_a? Symbol
    @description.each_with_index do |n, i|
      return @tarray[i] if n == index_or_name
    end
  end
  raise "Unknown element name: '#{index_or_name}'"
end

#to_aObject

Return content of tuple as array of elements. The order and names are contained in ‘description’. Use the #[] method to more robustly access individual elements.



14
15
16
# File 'lib/omf_rete/tuple.rb', line 14

def to_a
  @tarray
end

#to_hashObject

Return content of tuple as a hash. The key is taken from the ‘description’.



20
21
22
23
24
25
26
27
# File 'lib/omf_rete/tuple.rb', line 20

def to_hash
  h = {}
  @description.each_with_index do |n, i|
    name = n.to_s.chomp('?').to_sym
    h[name] = @tarray[i]
  end
  h
end