Class: Range
- Includes:
- ActiveSupport::CompareWithRange, ActiveSupport::EachTimeWithZone, ActiveSupport::RangeWithFormat
- Defined in:
- lib/active_support/core_ext/enumerable.rb,
lib/active_support/core_ext/object/json.rb,
lib/active_support/core_ext/range/overlap.rb
Overview
:nodoc:
Constant Summary
Constants included from ActiveSupport::RangeWithFormat
ActiveSupport::RangeWithFormat::RANGE_FORMATS
Instance Method Summary collapse
-
#as_json(options = nil) ⇒ Object
:nodoc:.
- #overlap?(other) ⇒ Boolean (also: #overlaps?)
-
#sum(initial_value = 0) ⇒ Object
Optimize range sum to use arithmetic progression if a block is not given and we have a range of numeric values.
Methods included from ActiveSupport::EachTimeWithZone
Methods included from ActiveSupport::RangeWithFormat
Methods included from ActiveSupport::CompareWithRange
Instance Method Details
#as_json(options = nil) ⇒ Object
:nodoc:
158 159 160 |
# File '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?
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File '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 |
#sum(initial_value = 0) ⇒ Object
Optimize range sum to use arithmetic progression if a block is not given and we have a range of numeric values.
241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File '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 |