Module: MusicBrainz::CoreExtensions::Range::Equality
- Included in:
- Range
- Defined in:
- lib/rbrainz/core_ext/range/equality.rb
Overview
Mixin module with equality operations for ranges. The built-in Range class is currently missing functions to compare a Range with another Range. There exist 13 disjoint equality operations for ranges. This mixin implements them all and a few extra operations for commonly used combinations.
Instance Method Summary collapse
-
#<=(b) ⇒ Object
a <= bis the same asa.before?(b) or a.meets_beginning_of?(b). -
#>=(b) ⇒ Object
a >= bis the same asa.after?(b) or a.meets_end_of?(b). -
#after?(b) ⇒ Boolean
(also: #>)
a.after?(b)is true, if a begins after the end of b. -
#before?(b) ⇒ Boolean
(also: #<)
a.before?(b)is true, if a ends before the beginning of b. -
#between?(b) ⇒ Boolean
a.between?(b)is the same asa.starts?(b) or a.during?(b) or a.finishes?(b). -
#contains?(b) ⇒ Boolean
a.contains?(b)is true, if b fits completely into a. -
#during?(b) ⇒ Boolean
a.during?(b)is true, if a fits completely into b. -
#finished_by?(b) ⇒ Boolean
a.finished_by?(b)is true, if a and b have the same end but b begins after a. -
#finishes?(b) ⇒ Boolean
a.finishes?(b)is true, if a and b have the same end but a begins after b. -
#meets_beginning_of?(b) ⇒ Boolean
a.meets_beginning_of?(b)is true, if b begins exactly at the end of a. -
#meets_end_of?(b) ⇒ Boolean
a.meets_end_of?(b)is true, if b ends exactly at the beginning of a. -
#overlaps_beginning_of?(b) ⇒ Boolean
a.overlaps_beginning_of?(b)is true, if a overlaps the beginning of b. -
#overlaps_end_of?(b) ⇒ Boolean
a.overlaps_end_of?(b)is true, if a overlaps the end of b. -
#started_by?(b) ⇒ Boolean
a.started_by?(b)is true, if a and b have the same beginning but a lasts longer than b. -
#starts?(b) ⇒ Boolean
a.starts?(b)is true, if a and b have the same beginning but b lasts longer than a.
Instance Method Details
#<=(b) ⇒ Object
a <= b is the same as a.before?(b) or a.meets_beginning_of?(b)
199 200 201 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 199 def <=(b) self.before?(b) || self.meets_beginning_of?(b) end |
#>=(b) ⇒ Object
a >= b is the same as a.after?(b) or a.meets_end_of?(b)
205 206 207 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 205 def >=(b) self.after?(b) || self.meets_end_of?(b) end |
#after?(b) ⇒ Boolean Also known as: >
a.after?(b) is true, if a begins after the end of b. Same as b.before?(a).
|A------|
|B------|
40 41 42 43 44 45 46 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 40 def after?(b) if b.kind_of? ::Range b.open_end < self.begin else b.succ < self.begin end end |
#before?(b) ⇒ Boolean Also known as: <
a.before?(b) is true, if a ends before the beginning of b. Same as b.after?(a).
|A------|
|B------|
26 27 28 29 30 31 32 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 26 def before?(b) if b.kind_of? ::Range self.open_end < b.begin else self.open_end < b end end |
#between?(b) ⇒ Boolean
a.between?(b) is the same as a.starts?(b) or a.during?(b) or a.finishes?(b)
211 212 213 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 211 def between?(b) self.starts?(b) || self.during?(b) || self.finishes?(b) end |
#contains?(b) ⇒ Boolean
a.contains?(b) is true, if b fits completely into a. Same as b.during?(a).
|A------------|
|B------|
133 134 135 136 137 138 139 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 133 def contains?(b) if b.kind_of? ::Range self.begin < b.begin && b.open_end < self.open_end else self.begin < b && b.succ < self.open_end end end |
#during?(b) ⇒ Boolean
a.during?(b) is true, if a fits completely into b. Same as b.contains?(a).
|A------|
|B------------|
120 121 122 123 124 125 126 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 120 def during?(b) if b.kind_of? ::Range b.begin < self.begin && self.open_end < b.open_end else false end end |
#finished_by?(b) ⇒ Boolean
a.finished_by?(b) is true, if a and b have the same end but b begins after a. Same as b.finishes?(a).
|A-----------|
|B------|
189 190 191 192 193 194 195 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 189 def finished_by?(b) if b.kind_of? ::Range self.begin < b.begin && b.open_end == self.open_end else self.begin < b && b.succ == self.open_end end end |
#finishes?(b) ⇒ Boolean
a.finishes?(b) is true, if a and b have the same end but a begins after b. Same as b.finished_by?(a).
|A------|
|B-----------|
175 176 177 178 179 180 181 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 175 def finishes?(b) if b.kind_of? ::Range b.begin < self.begin && self.open_end == b.open_end else false end end |
#meets_beginning_of?(b) ⇒ Boolean
a.meets_beginning_of?(b) is true, if b begins exactly at the end of a. Same as b.meets_end_of?(a).
|A------|
|B------|
68 69 70 71 72 73 74 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 68 def meets_beginning_of?(b) if b.kind_of? ::Range self.open_end == b.begin else self.open_end == b end end |
#meets_end_of?(b) ⇒ Boolean
a.meets_end_of?(b) is true, if b ends exactly at the beginning of a. Same as b.meets_beginning_of?(a).
|A------|
|B------|
81 82 83 84 85 86 87 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 81 def meets_end_of?(b) if b.kind_of? ::Range b.open_end == self.begin else b.succ == self.begin end end |
#overlaps_beginning_of?(b) ⇒ Boolean
a.overlaps_beginning_of?(b) is true, if a overlaps the beginning of b. Same as b.overlaps_end_of?(a).
|A------|
|B------|
94 95 96 97 98 99 100 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 94 def overlaps_beginning_of?(b) if b.kind_of? ::Range self.begin < b.begin && self.open_end < b.open_end && b.begin < self.open_end else false end end |
#overlaps_end_of?(b) ⇒ Boolean
a.overlaps_end_of?(b) is true, if a overlaps the end of b. Same as b.overlaps_beginning_of?(a).
|A------|
|B------|
107 108 109 110 111 112 113 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 107 def overlaps_end_of?(b) if b.kind_of? ::Range b.begin < self.begin && b.open_end < self.open_end && self.begin < b.open_end else false end end |
#started_by?(b) ⇒ Boolean
a.started_by?(b) is true, if a and b have the same beginning but a lasts longer than b. Same as b.starts?(a).
|A-----------|
|B------|
161 162 163 164 165 166 167 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 161 def started_by?(b) if b.kind_of? ::Range b.begin == self.begin && b.open_end < self.open_end else b == self.begin && b.succ < self.open_end end end |
#starts?(b) ⇒ Boolean
a.starts?(b) is true, if a and b have the same beginning but b lasts longer than a. Same as b.started_by?(a).
|A------|
|B-----------|
147 148 149 150 151 152 153 |
# File 'lib/rbrainz/core_ext/range/equality.rb', line 147 def starts?(b) if b.kind_of? ::Range self.begin == b.begin && self.open_end < b.open_end else false end end |