Class: TruthTable::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/truth-table.rb

Instance Method Summary collapse

Constructor Details

#initialize(expression, variables) ⇒ Evaluator

Returns a new instance of Evaluator.



21
22
23
24
25
26
27
# File 'lib/truth-table.rb', line 21

def initialize(expression, variables)
  @expression = expression
  variables.each_pair do |k,v|
    instance_variable_set( "@" + k.to_s, v)
    create_attr(k)
  end
end

Instance Method Details

#create_attr(name) ⇒ Object



15
16
17
18
19
# File 'lib/truth-table.rb', line 15

def create_attr( name )
    create_method( name.to_sym ) { 
        instance_variable_get( "@" + name.to_s ) 
    }
end

#create_method(name, &block) ⇒ Object



11
12
13
# File 'lib/truth-table.rb', line 11

def create_method( name, &block )
    self.class.send( :define_method, name, &block )
end

#evaluateObject



29
30
31
# File 'lib/truth-table.rb', line 29

def evaluate
  eval(@expression)
end