Class: Uttk::Weights::Weight

Inherits:
Object
  • Object
show all
Defined in:
lib/uttk/weights/Weight.rb

Overview

FIXME: document me

Direct Known Subclasses

WExpr, WFloat

Constant Summary collapse

BASE =
{
  :START => [0, 0, 0],
  :PASS  => [1, 0, 1],
  :FAIL  => [0, 0, 1]
}.freeze.each_value { |x| x.freeze }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aFloat, min = 0, max = 1) ⇒ Weight

Returns a new instance of Weight.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/uttk/weights/Weight.rb', line 19

def initialize ( aFloat, min=0, max=1 )
  if BASE.has_key? aFloat
    aFloat, min, max = BASE[aFloat]
  end
  aFloat = aFloat.to_f
  @val = aFloat
  @min, @max = min, max
  unless @min <= aFloat or aFloat <= @max
    raise ArgumentError, "#@min <= #{aFloat} <= #@max"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*a, &b) ⇒ Object



97
98
99
# File 'lib/uttk/weights/Weight.rb', line 97

def method_missing ( *a, &b )
  @val.send(*a, &b)
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



17
18
19
# File 'lib/uttk/weights/Weight.rb', line 17

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



17
18
19
# File 'lib/uttk/weights/Weight.rb', line 17

def min
  @min
end

#valObject (readonly) Also known as: get

Returns the value of attribute val.



83
84
85
# File 'lib/uttk/weights/Weight.rb', line 83

def val
  @val
end

Instance Method Details

#*(rhs) ⇒ Object



63
64
65
66
67
68
# File 'lib/uttk/weights/Weight.rb', line 63

def * ( rhs )
  f = rhs.to_f
  a, b, c = to_f * f, @min * f, @max * f
  b, c = c, b if f < 0
  self.class.new(a, b, c)
end

#+(rhs) ⇒ Object



58
59
60
61
# File 'lib/uttk/weights/Weight.rb', line 58

def + ( rhs )
  rhs = self.class.new(rhs) unless rhs.is_a? Weight
  self.class.new(to_f + rhs.to_f, @min + rhs.min, @max + rhs.max)
end

#==(rhs) ⇒ Object



90
91
92
93
94
95
# File 'lib/uttk/weights/Weight.rb', line 90

def == ( rhs )
  rhs.class <= Weight and
  @min == rhs.min and
  @val == rhs.val and
  @max == rhs.max
end

#fail?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/uttk/weights/Weight.rb', line 54

def fail?
  to_f != @max
end

#long_getObject



86
87
88
# File 'lib/uttk/weights/Weight.rb', line 86

def long_get
  "#{get}[#@min, #@max]"
end

#max?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/uttk/weights/Weight.rb', line 46

def max?
  to_f == @max
end

#min?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/uttk/weights/Weight.rb', line 42

def min?
  to_f == @min
end

#normalize(weight) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/uttk/weights/Weight.rb', line 70

def normalize ( weight )
  diff = @max - @min
  if diff.zero?
    if @max.zero? and weight.zero?
      self.class.new(:PASS)
    else
      self.class.new(:FAIL)
    end
  else
    self.class.new((self - @min) / diff, 0, 1)
  end
end

#pass?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/uttk/weights/Weight.rb', line 50

def pass?
  ! zero? and max?
end

#start?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/uttk/weights/Weight.rb', line 38

def start?
  [self, @max, @min].all? { |x| x.zero? }
end