Class: MinMax

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/min_max.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(min = nil, max = nil) ⇒ MinMax

Returns a new instance of MinMax.



4
5
6
7
8
# File 'lib/core_ext/min_max.rb', line 4

def initialize(min = nil, max = nil)
  min ||= 0.0
  max ||= min
  @min, @max = (min <= max) ? [min, max] : [max, min]
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



2
3
4
# File 'lib/core_ext/min_max.rb', line 2

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



2
3
4
# File 'lib/core_ext/min_max.rb', line 2

def min
  @min
end

Instance Method Details

#*(oth) ⇒ Object

HACK: use a more clever solution



19
20
21
22
# File 'lib/core_ext/min_max.rb', line 19

def *(oth)
  v = possible_values(self, oth){ |a, b| a * b }
  MinMax.new(v.min, v.max)
end

#+(oth) ⇒ Object



10
11
12
# File 'lib/core_ext/min_max.rb', line 10

def +(oth)
  MinMax.new(self.min + oth.min, self.max + oth.max)
end

#-(oth) ⇒ Object



14
15
16
# File 'lib/core_ext/min_max.rb', line 14

def -(oth)
  MinMax.new(self.min - oth.max, self.max - oth.min)
end

#/(oth) ⇒ Object

HACK: use a more clever solution



25
26
27
28
29
30
# File 'lib/core_ext/min_max.rb', line 25

def /(oth)
  raise "We can not divide by a range of: #{oth.min} to #{oth.max}" if (oth.min * oth.max) < 0

  v = possible_values(self, oth){ |a, b| a / b }
  MinMax.new(v.min, v.max)
end

#randomObject

Returns: A random value between min and max



33
34
35
# File 'lib/core_ext/min_max.rb', line 33

def random
  (min == max) ? min : rand * (max - min) + min
end