module CoreExtensions::Range::Operations

Public Class Methods

included(klass) click to toggle source
# File lib/core_extensions/range/operations.rb, line 10
def self.included(klass)
  klass.extend(ClassMethods)
end

Public Instance Methods

-(other) click to toggle source

Calls {ClassMethods#subtract} with ‘self` and `other`

@param [Range] other The range to subtract.

@return [Array<Range>] An array of ranges obtained from subtracting ‘other` from `self`.

# File lib/core_extensions/range/operations.rb, line 43
def -(other)
  self.class.subtract(self, other)
end
merge(other) click to toggle source

Calls {ClassMethods#merge} with ‘self` and `*others`

@param [Range] other The range to merge.

@return [Array<Range>] An array of merged ranges.

# File lib/core_extensions/range/operations.rb, line 52
def merge(other)
  self.class.merge(self, other)
end
to_closed_range(delta: 1) click to toggle source

Returns a range whose end is included in the range.

@param delta The amount which should be subtracted from the range end when converting

to a closed range.

@return [Range] self if the range is already closed, otherwise return a new range

where the `begin` is the same as `self`, and the `end` is `self.end - delta`.

@example Convert an open time range to a closed time range.

(Time.new(2020)...Time.new(2021)).to_closed_range #=> 2020-01-01 00:00:00 -0800..2020-12-31 23:59:59 -0800

@example Convert an open time range to a closed time range and specify the delta.

(Time.new(2020)...Time.new(2021)).to_closed_range(delta: Float::EPSILON)
#=> 2020-01-01 00:00:00 -0800..2020-12-31 23:59:59 4503599627370495/4503599627370496 -0800
# File lib/core_extensions/range/operations.rb, line 28
def to_closed_range(delta: 1)
  return self unless exclude_end?

  if delta.is_a?(Proc)
    (self.begin..(delta.call(self.end)))
  else
    (self.begin..self.end - delta)
  end
end