Class: Range

Inherits:
Object
  • Object
show all
Defined in:
lib/bookie/extensions.rb

Overview

Reopened to add some useful methods

Instance Method Summary collapse

Instance Method Details

#empty?Boolean

Returns whether the range is empty

A range is empty if end < begin or if begin == end and exclude_end? is true.

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/bookie/extensions.rb', line 33

def empty?
  if exclude_end?
    self.end <= self.begin
  else
    self.end < self.begin
  end
end

#exclusiveObject

Converts the range to an equivalent exclusive range (one where exclude_end? is true)

Only works for ranges with discrete steps between values (i.e. integers)



21
22
23
24
25
26
27
# File 'lib/bookie/extensions.rb', line 21

def exclusive
  if exclude_end?
    self
  else
    Range.new(self.begin, self.end + 1, true)
  end
end

#normalizedObject

If end < begin, returns an empty range (begin … begin) Otherwise, returns the original range



9
10
11
12
13
14
15
# File 'lib/bookie/extensions.rb', line 9

def normalized
 if self.end < self.begin
   self.begin ... self.begin
 else
  self
 end
end