Class: Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/fathom/archive/n2.rb,
lib/fathom/archive/n3.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, *values) ⇒ Variable

Returns a new instance of Variable.



35
36
37
38
39
40
41
# File 'lib/fathom/archive/n2.rb', line 35

def initialize(name, *values)
  values = [true, false] if values.empty?
  @name = name
  @values = values
  @observations = Array.new(@values.size, 0)
  @total = 0
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



33
34
35
# File 'lib/fathom/archive/n2.rb', line 33

def name
  @name
end

#observationsObject (readonly)

Returns the value of attribute observations.



33
34
35
# File 'lib/fathom/archive/n2.rb', line 33

def observations
  @observations
end

#totalObject (readonly)

Returns the value of attribute total.



33
34
35
# File 'lib/fathom/archive/n2.rb', line 33

def total
  @total
end

#valuesObject (readonly)

Returns the value of attribute values.



33
34
35
# File 'lib/fathom/archive/n2.rb', line 33

def values
  @values
end

Class Method Details

.infer(obj, *values) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fathom/archive/n2.rb', line 68

def infer(obj, *values)
  return obj if obj.is_a?(Variable)
  case obj
  when Symbol
    Variable.new(obj, *values)
  when String
    Variable.new(obj.to_sym, *values)
  else
    nil
  end
end

Instance Method Details

#inspectObject



63
64
65
# File 'lib/fathom/archive/n2.rb', line 63

def inspect
  "Variable: #{self.name} #{self.values.inspect}"
end

#observe(value = nil) ⇒ Object

You can observe anything but nothing: we record any observation but nil.

If nil is set, we use the first value as the default.



45
46
47
48
49
50
51
52
53
54
# File 'lib/fathom/archive/n2.rb', line 45

def observe(value=nil)
  value = self.values.first if value.nil?
  unless self.values.include?(value)
    self.values << value 
    self.observations << 0
  end
  index = self.values.index(value)
  self.observations[index] += 1
  @total += 1
end

#observed(value) ⇒ Object

Lookup observations



57
58
59
60
61
# File 'lib/fathom/archive/n2.rb', line 57

def observed(value)
  index = self.values.index(value)
  return 0 unless index
  self.observations[index]
end