Class: TTK::Weights::Weight

Inherits:
Float
  • Object
show all
Defined in:
lib/ttk/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/ttk/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
  super(aFloat)
  @min, @max = min, max
  unless @min <= aFloat or aFloat <= @max
    raise ArgumentError, "#@min <= #{aFloat} <= #@max"
  end
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



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

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



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

def min
  @min
end

Instance Method Details

#*(rhs) ⇒ Object



55
56
57
58
59
60
# File 'lib/ttk/weights/Weight.rb', line 55

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



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

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

#fail?Boolean

Returns:

  • (Boolean)


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

def fail?
  to_f != @max
end

#long_getObject



77
78
79
# File 'lib/ttk/weights/Weight.rb', line 77

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

#normalize(weight) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ttk/weights/Weight.rb', line 62

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)


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

def pass?
  ! zero? and to_f == @max
end

#start?Boolean

Returns:

  • (Boolean)


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

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