Class: Range
- Includes:
- ActiveSupport::CompareWithRange, ActiveSupport::DeprecatedRangeWithFormat, 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/overlaps.rb
Overview
:nodoc:
Constant Summary
Constants included from ActiveSupport::RangeWithFormat
ActiveSupport::RangeWithFormat::RANGE_FORMATS
Constants included from ActiveSupport::DeprecatedRangeWithFormat
ActiveSupport::DeprecatedRangeWithFormat::NOT_SET
Instance Method Summary collapse
-
#as_json(options = nil) ⇒ Object
:nodoc:.
-
#overlaps?(other) ⇒ Boolean
Compare two ranges and see if they overlap each other (1..5).overlaps?(4..6) # => true (1..5).overlaps?(7..9) # => false.
-
#sum(identity = nil) ⇒ 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
Methods included from ActiveSupport::DeprecatedRangeWithFormat
Instance Method Details
#as_json(options = nil) ⇒ Object
:nodoc:
152 153 154 |
# File 'lib/active_support/core_ext/object/json.rb', line 152 def as_json( = nil) # :nodoc: to_s end |
#overlaps?(other) ⇒ Boolean
Compare two ranges and see if they overlap each other
(1..5).overlaps?(4..6) # => true
(1..5).overlaps?(7..9) # => false
7 8 9 |
# File 'lib/active_support/core_ext/range/overlaps.rb', line 7 def overlaps?(other) other.begin == self.begin || cover?(other.begin) || other.cover?(self.begin) end |
#sum(identity = nil) ⇒ Object
Optimize range sum to use arithmetic progression if a block is not given and we have a range of numeric values.
287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/active_support/core_ext/enumerable.rb', line 287 def sum(identity = nil) 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 = identity || 0 sum + (actual_last - first + 1) * (actual_last + first) / 2 else identity || 0 end end end |