Class: Range
- Includes:
- Random::RangeExtensions
- Defined in:
- lib/standard/facets/random.rb,
lib/core/facets/range/nudge.rb,
lib/core/facets/range/op_add.rb,
lib/core/facets/range/op_sub.rb,
lib/core/facets/range/to_rng.rb,
lib/core/facets/range/within.rb,
lib/core/facets/range/combine.rb,
lib/core/facets/range/quantile.rb,
lib/core/facets/range/intersection.rb
Class Method Summary collapse
-
.combine(*intervals) ⇒ Object
Combine intervals.
-
.intersection(*ranges) ⇒ Object
Returns the intersection of two or more ranges — the region they all share.
Instance Method Summary collapse
-
#+(value) ⇒ Object
Add two ranges to create a range array.
-
#-(value) ⇒ Object
Subtract one range from another producing a range array.
-
#combine(*intervals) ⇒ Object
Combine ranges.
-
#intersection(*others) ⇒ Object
Returns the intersection of this range with one or more other ranges.
-
#nudge(value = 1, min: nil, max: nil) ⇒ Object
Nudge range values.
-
#quantile(k, n = 100) ⇒ Integer
Calculate the kth n-tile in a range.
-
#to_range ⇒ Object
A thing really should know itself.
-
#to_rng ⇒ Object
A thing really should know itself.
-
#umbrella(r) ⇒ Object
Returns a two element array of the relationship between two Ranges.
-
#within?(rng) ⇒ Boolean
Uses the Range#umbrella method to determine if another Range is anywhere within this Range.
Methods included from Random::RangeExtensions
Class Method Details
.combine(*intervals) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/core/facets/range/combine.rb', line 23 def self.combine(*intervals) intype = intervals.first.class result = [] intervals = intervals.collect do |i| [i.first, i.last] end intervals.sort.each do |(from, to)| #inject([]) do |result, if result.empty? or from > result.last[1] result << [from, to] elsif to > result.last[1] result.last[1] = to end #result end if intype <= Range result.collect{ |i| ((i.first)..(i.last)) } else result end end |
.intersection(*ranges) ⇒ Object
Returns the intersection of two or more ranges — the region they all share. Returns nil if the ranges do not overlap.
Works with any comparable type: integers, floats, dates, strings, etc.
Examples
Range.intersection(1..10, 5..15) #=> 5..10
Range.intersection(1..5, 3..7, 4..9) #=> 4..5
Range.intersection(1..3, 5..7) #=> nil
Range.intersection(1.0..5.0, 2.5..4.5) #=> 2.5..4.5
Also available as an instance method:
(1..10).intersection(5..15) #=> 5..10
CREDIT: Trans
21 22 23 24 25 26 27 28 29 |
# File 'lib/core/facets/range/intersection.rb', line 21 def self.intersection(*ranges) return nil if ranges.empty? ranges.reduce do |result, r| return nil unless result.overlap?(r) new_first = result.first > r.first ? result.first : r.first new_last = result.last < r.last ? result.last : r.last new_first..new_last end end |
Instance Method Details
#+(value) ⇒ Object
Add two ranges to create a range array.
Returns [Array]
CREDIT: monocle
11 12 13 |
# File 'lib/core/facets/range/op_add.rb', line 11 def +(value) [self, value].arrange end |
#-(value) ⇒ Object
Subtract one range from another producing a range array.
Examples
(1..10) - (4..6) => [1..3, 7..10]
(1..10) - (9..12) => [1..8]
Returns [Array]
CREDIT: monocle
15 16 17 18 19 20 21 |
# File 'lib/core/facets/range/op_sub.rb', line 15 def -(value) if value.class == first.class minus_obj(value) else [minus_obj(value.first)[0], minus_obj(value.last)[1]].compact end end |
#combine(*intervals) ⇒ Object
Combine ranges.
(1..2).combine(2..4) #=> [1..4]
(1..2).combine(3..4) #=> [1..2, 3..4]
TODO: Incorporate end-sentinal inclusion vs. exclusion.
CREDIT: Trans
12 13 14 |
# File 'lib/core/facets/range/combine.rb', line 12 def combine(*intervals) Range.combine(self, *intervals) end |
#intersection(*others) ⇒ Object
Returns the intersection of this range with one or more other ranges. Returns nil if the ranges do not overlap.
(1..10).intersection(5..15) #=> 5..10
(1..10).intersection(5..15, 8..20) #=> 8..10
(1..3).intersection(5..7) #=> nil
38 39 40 |
# File 'lib/core/facets/range/intersection.rb', line 38 def intersection(*others) Range.intersection(self, *others) end |
#nudge(value = 1, min: nil, max: nil) ⇒ Object
Nudge range values
(1..5).nudge #=> 2..6
(1..5).nudge(2) #=> 3..7
(1..5).nudge(-2) #=> -1..3
(1..5).nudge(min: 1) #=> 2..5
(1..5).nudge(max: 1) #=> 1..6
CREDIT: Codeindulgence
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/core/facets/range/nudge.rb', line 13 def nudge(value = 1, min: nil, max: nil) if min or max min ||= 0 max ||= 0 else min = max = value end if exclude_end? (self.min + min)...((self.max + 1) + max) else (self.min + min)..(self.max + max) end end |
#quantile(k, n = 100) ⇒ Integer
Calculate the kth n-tile in a range.
If n=4 the quantity is called a quartile, and if n=100 it is called a percentile.
12 13 14 15 16 |
# File 'lib/core/facets/range/quantile.rb', line 12 def quantile(k, n=100) return 1 if k < first return n if k >= last ((k - first) / ((last - first) / n.to_f)).to_i + 1 end |
#to_range ⇒ Object
A thing really should know itself. This simply returns self.
Note: This does not internally effect the Ruby interpretor such that it can coerce Range-like objects into a Range.
CREDIT: Trans
21 22 23 |
# File 'lib/core/facets/range/to_rng.rb', line 21 def to_range self end |
#to_rng ⇒ Object
A thing really should know itself. This simply returns self.
CREDIT: Trans
8 9 10 |
# File 'lib/core/facets/range/to_rng.rb', line 8 def to_rng self end |
#umbrella(r) ⇒ Object
Returns a two element array of the relationship between two Ranges.
Diagram …
Relationship Returns
self |-----------|
r |-----------| [0,0]
self |-----------|
r |---------| [-1,-1]
self |---------|
r |-----------| [1,1]
self |-----------|
r |----------| [-1,0]
self |-----------|
r |-----------| [-1,1]
etc.
Example:
(0..3).umbrella(1..2) #=> [-1,-1]
CREDIT: Trans, Chris Kappler
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/core/facets/range/within.rb', line 49 def umbrella(r) s = first <=> r.first e = r.last <=> last if e == 0 if r.exclude_end? and exclude_end? e = r.max <=> max else e = (r.exclude_end? ? 0 : 1) <=> (exclude_end? ? 0 : 1) end end return s,e end |
#within?(rng) ⇒ Boolean
Uses the Range#umbrella method to determine if another Range is anywhere within this Range.
(1..3).within?(0..4) #=> true
CREDIT: Trans
10 11 12 13 14 15 16 17 |
# File 'lib/core/facets/range/within.rb', line 10 def within?(rng) case rng.umbrella(self) when [0,0], [-1,0], [0,-1], [-1,-1] return true else return false end end |