class Nutriscore::Common::Range

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).

Public Class Methods

wrap(a) click to toggle source

Returns a {{Nutriscore::Common::Range}} object from a {{Numeric}} or {{Range}}.

# 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

Public Instance Methods

*(a) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
Calls superclass method
# File lib/nutriscore/common/range.rb, line 61
def ==(a)
  if Numeric === a
    single == a
  else
    super
  end
end
inspect() click to toggle source
# File lib/nutriscore/common/range.rb, line 77
def inspect
  to_s
end
single() click to toggle source

Returns single value if possible, nil if there is a range of values.

# File lib/nutriscore/common/range.rb, line 82
def single
  min if min == max
end
to_s() click to toggle source
Calls superclass method
# File lib/nutriscore/common/range.rb, line 69
def to_s
  if min == max
    min.to_s
  else
    super
  end
end