Class: MinMax
- Inherits:
-
Object
- Object
- MinMax
- Defined in:
- lib/core_ext/min_max.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
Instance Method Summary collapse
-
#*(oth) ⇒ Object
HACK: use a more clever solution.
- #+(oth) ⇒ Object
- #-(oth) ⇒ Object
-
#/(oth) ⇒ Object
HACK: use a more clever solution.
-
#initialize(min = nil, max = nil) ⇒ MinMax
constructor
A new instance of MinMax.
-
#random ⇒ Object
Returns: A random value between min and max.
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
#max ⇒ Object (readonly)
Returns the value of attribute max.
2 3 4 |
# File 'lib/core_ext/min_max.rb', line 2 def max @max end |
#min ⇒ Object (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 |
#random ⇒ Object
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 |