Class: Nutriscore::Common::Range
- Inherits:
-
Range
- Object
- Range
- Nutriscore::Common::Range
- Defined in:
- lib/nutriscore/common/range.rb
Overview
Range class that supports addition, substraction and comparison. Assumes the objects that the range is composed of is a {Numeric}.
Note that the end range is always included (exclude_end
is false
).
Class Method Summary collapse
-
.wrap(a) ⇒ Object
Returns a {Nutriscore{Nutriscore::Common{Nutriscore::Common::Range} object from a {Numeric} or {Range}.
Instance Method Summary collapse
- #*(a) ⇒ Object
- #+(a) ⇒ Object
- #-(a) ⇒ Object
- #/(a) ⇒ Object
- #==(a) ⇒ Object
- #inspect ⇒ Object
-
#single ⇒ Object
Returns single value if possible,
nil
if there is a range of values. - #to_s ⇒ Object
Class Method Details
.wrap(a) ⇒ Object
Returns a {Nutriscore{Nutriscore::Common{Nutriscore::Common::Range} object from a {Numeric} or {Range}.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/nutriscore/common/range.rb', line 9 def self.wrap(a) if Numeric === a Range.new(a, a) elsif Range === a a elsif ::Range === a Range.new(a.first, a.last) else raise ArgumentError end end |
Instance Method Details
#*(a) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/nutriscore/common/range.rb', line 41 def *(a) if Numeric === a Range.new(min * a, max * a) elsif ::Range === a Range.new(min * a.min, max * a.max) else raise ArgumentError end end |
#+(a) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/nutriscore/common/range.rb', line 21 def +(a) if Numeric === a Range.new(min + a, max + a) elsif ::Range === a Range.new(min + a.min, max + a.max) else raise ArgumentError end end |
#-(a) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/nutriscore/common/range.rb', line 31 def -(a) if Numeric === a Range.new(min - a, max - a) elsif ::Range === a Range.new(min - a.max, max - a.min) else raise ArgumentError end end |
#/(a) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/nutriscore/common/range.rb', line 51 def /(a) if Numeric === a Range.new(min / a, max / a) elsif ::Range === a Range.new(min / a.max, max / a.min) else raise ArgumentError end end |
#==(a) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/nutriscore/common/range.rb', line 61 def ==(a) if Numeric === a single == a else super end end |
#inspect ⇒ Object
77 78 79 |
# File 'lib/nutriscore/common/range.rb', line 77 def inspect to_s end |
#single ⇒ Object
Returns single value if possible, nil
if there is a range of values.
82 83 84 |
# File 'lib/nutriscore/common/range.rb', line 82 def single min if min == max end |
#to_s ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/nutriscore/common/range.rb', line 69 def to_s if min == max min.to_s else super end end |