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, activesupport/lib/active_support/core_ext/range/sole.rb |
Constant Summary
::ActiveSupport::RangeWithFormat - Prepended
Instance Method Summary
- #overlaps?
-
#sole
Returns the sole item in the range.
- #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::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
#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
#overlaps?
[ GitHub ]# File 'activesupport/lib/active_support/core_ext/range/overlap.rb', line 4
alias :overlaps? :overlap?
#sole
Returns the sole item in the range. If there are no items, or more than one item, raises ::Enumerable::SoleItemExpectedError.
(1..1).sole # => 1
(2..1).sole # => Enumerable::SoleItemExpectedError: no item found
(..1).sole # => Enumerable::SoleItemExpectedError: infinite range cannot represent a sole item
# File 'activesupport/lib/active_support/core_ext/range/sole.rb', line 10
def sole if self.begin.nil? || self.end.nil? raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "infinite range '#{inspect}' cannot represent a sole item" end super end
#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 253
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