Class: Upl::Variables

Inherits:
Hash show all
Defined in:
lib/upl/variables.rb

Overview

Storage from variables, where setting one just calls unify on the underlying terms. Cos it’s hard to hang unify on a single variable.

Instance Method Summary collapse

Constructor Details

#initialize(*names) ⇒ Variables

Returns a new instance of Variables.



5
6
7
8
9
10
# File 'lib/upl/variables.rb', line 5

def initialize *names
  super
  names.each do |name|
    self.store name, Variable.new
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/upl/variables.rb', line 17

def method_missing meth, *args
  # unfreeze
  name = meth.to_s.dup

  the_method =
  if name.chomp! '='
    # set the value
    :'[]='
  else
    # fetch the value
    :'[]'
  end

  var_name = name.to_sym

  if has_key? var_name
    send the_method, var_name, *args
  else
    super
  end
end

Instance Method Details

#[]=(name, term) ⇒ Object

calls unify, so you can’t set a given variable more than once.



13
14
15
# File 'lib/upl/variables.rb', line 13

def []=( name, term )
  Extern::PL_unify self[name.to_sym].to_term_t, term.to_term_t
end

#pretty_print(pp) ⇒ Object



39
40
41
# File 'lib/upl/variables.rb', line 39

def pretty_print pp
  transform_values{|v| v.to_ruby}.pretty_print pp
end