Class: Range

Inherits:
Object show all
Defined in:
lib/utilities/range.rb

Instance Method Summary collapse

Instance Method Details

#cover?(object) ⇒ Boolean

Adds cover? if not defined (like in previous rubies)

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/utilities/range.rb', line 20

def cover? object
  ends = [self.first, self.last]
  ends.min <= object && object <= ends.max
end

#empty?Boolean

Verify if self is empty

Returns:

  • (Boolean)


10
11
12
# File 'lib/utilities/range.rb', line 10

def empty?
  count.zero?
end

#intersection(range) ⇒ Object Also known as: &

Return a range containing elements common to the two ranges, with no duplicates



3
4
5
6
# File 'lib/utilities/range.rb', line 3

def intersection range
  values = self.to_a & range.to_a
  values.empty? ? nil : (values.first..values.last)  
end

#overlap?(range) ⇒ Boolean

Detect if the two ranges overlap one with the other

Returns:

  • (Boolean)


15
16
17
# File 'lib/utilities/range.rb', line 15

def overlap? range
  !(self & range).nil?
end