Class: Range

Inherits:
Object show all
Defined in:
motion/core_ext/enumerable.rb,
motion/core_ext/range/overlaps.rb,
motion/core_ext/range/include_range.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#include_with_range?(value) ⇒ Boolean

Extends the default Range#include? to support range comparisons.

(1..5).include?(1..5) # => true
(1..5).include?(2..3) # => true
(1..5).include?(2..6) # => false

The native Range#include? behavior is untouched.

('a'..'f').include?('c') # => true
(5..9).include?(11) # => false

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
# File 'motion/core_ext/range/include_range.rb', line 10

def include_with_range?(value)
  if value.is_a?(::Range)
    # 1...10 includes 1..9 but it does not include 1..10.
    operator = exclude_end? && !value.exclude_end? ? :< : :<=
    include_without_range?(value.first) && value.last.send(operator, last)
  else
    include_without_range?(value)
  end
end

#overlaps?(other) ⇒ Boolean

Compare two ranges and see if they overlap each other

(1..5).overlaps?(4..6) # => true
(1..5).overlaps?(7..9) # => false

Returns:

  • (Boolean)


5
6
7
# File 'motion/core_ext/range/overlaps.rb', line 5

def overlaps?(other)
  cover?(other.first) || other.cover?(first)
end

#sum(identity = 0) ⇒ Object

Optimize range sum to use arithmetic progression if a block is not given and we have a range of numeric values.



78
79
80
81
82
83
84
85
86
87
88
89
# File 'motion/core_ext/enumerable.rb', line 78

def sum(identity = 0)
  if block_given? || !(first.is_a?(Integer) && last.is_a?(Integer))
    super
  else
    actual_last = exclude_end? ? (last - 1) : last
    if actual_last >= first
      (actual_last - first + 1) * (actual_last + first) / 2
    else
      identity
    end
  end
end