Class: Range
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | activesupport/lib/active_support/core_ext/enumerable.rb, activesupport/lib/active_support/core_ext/object/json.rb, activesupport/lib/active_support/core_ext/range/overlap.rb |
Constant Summary
::ActiveSupport::RangeWithFormat
- Prepended
Instance Method Summary
- #overlap?(other) ⇒ Boolean (also: #overlaps?)
-
#overlaps?(other)
Alias for #overlap?.
- #_empty_range?(b, e, excl) ⇒ Boolean private
- #as_json(options = nil) Internal use only
-
#sum(initial_value = 0)
Internal use only
Optimize range sum to use arithmetic progression if a block is not given and we have a range of numeric values.
::ActiveSupport::EachTimeWithZone
- Prepended
::ActiveSupport::RangeWithFormat
- Prepended
#to_formatted_s | Alias for ActiveSupport::RangeWithFormat#to_fs. |
#to_fs | Convert range to a formatted string. |
::ActiveSupport::CompareWithRange
- Prepended
Instance Method Details
#_empty_range?(b, e, excl) ⇒ Boolean
(private)
# File 'activesupport/lib/active_support/core_ext/range/overlap.rb', line 31
def _empty_range?(b, e, excl) return false if b.nil? || e.nil? comp = b <=> e comp.nil? || comp > 0 || (comp == 0 && excl) end
#as_json(options = nil)
This method is for internal use only.
[ GitHub ]
# File 'activesupport/lib/active_support/core_ext/object/json.rb', line 158
def as_json( = nil) # :nodoc: to_s end
#overlap?(other) ⇒ Boolean
Also known as: #overlaps?
# File 'activesupport/lib/active_support/core_ext/range/overlap.rb', line 8
def overlap?(other) raise TypeError unless other.is_a? Range self_begin = self.begin other_end = other.end other_excl = other.exclude_end? return false if _empty_range?(self_begin, other_end, other_excl) other_begin = other.begin self_end = self.end self_excl = self.exclude_end? return false if _empty_range?(other_begin, self_end, self_excl) return true if self_begin == other_begin return false if _empty_range?(self_begin, self_end, self_excl) return false if _empty_range?(other_begin, other_end, other_excl) true end
#overlaps?(other)
Alias for #overlap?.
# File 'activesupport/lib/active_support/core_ext/range/overlap.rb', line 39
alias :overlaps? :overlap?
#sum(initial_value = 0)
This method is for internal use only.
Optimize range sum to use arithmetic progression if a block is not given and we have a range of numeric values.
# File 'activesupport/lib/active_support/core_ext/enumerable.rb', line 241
def sum(initial_value = 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 sum = initial_value || 0 sum + (actual_last - first + 1) * (actual_last + first) / 2 else initial_value || 0 end end end