Class: Range

Inherits:
Object show all
Defined in:
activesupport/lib/active_support/core_ext/enumerable.rb,
activesupport/lib/active_support/core_ext/range/each.rb,
activesupport/lib/active_support/core_ext/object/json.rb,
activesupport/lib/active_support/core_ext/range/overlaps.rb,
activesupport/lib/active_support/core_ext/range/conversions.rb,
activesupport/lib/active_support/core_ext/range/include_range.rb

Overview

:nodoc:

Constant Summary collapse

RANGE_FORMATS =
{
  :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
}

Instance Method Summary collapse

Instance Method Details

#as_json(options = nil) ⇒ Object

:nodoc:



133
134
135
# File 'activesupport/lib/active_support/core_ext/object/json.rb', line 133

def as_json(options = nil) #:nodoc:
  to_s
end

#each_with_time_with_zone(&block) ⇒ Object



5
6
7
8
# File 'activesupport/lib/active_support/core_ext/range/each.rb', line 5

def each_with_time_with_zone(&block)
  ensure_iteration_allowed
  each_without_time_with_zone(&block)
end

#include_with_range?(value) ⇒ Boolean

Extends the default Range#include? to support range comparisons.

(1..5).include?(1..5) # => true
(1..5).include?(2..3) # => true
(1..5).include?(2..6) # => false

The native Range#include? behavior is untouched.

('a'..'f').include?('c') # => true
(5..9).include?(11) # => false

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
# File 'activesupport/lib/active_support/core_ext/range/include_range.rb', line 12

def include_with_range?(value)
  if value.is_a?(::Range)
    # 1...10 includes 1..9 but it does not include 1..10.
    operator = exclude_end? && !value.exclude_end? ? :< : :<=
    include_without_range?(value.first) && value.last.send(operator, last)
  else
    include_without_range?(value)
  end
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

Returns:

  • (Boolean)


5
6
7
# File 'activesupport/lib/active_support/core_ext/range/overlaps.rb', line 5

def overlaps?(other)
  cover?(other.first) || other.cover?(first)
end

#step_with_time_with_zone(n = 1, &block) ⇒ Object



11
12
13
14
# File 'activesupport/lib/active_support/core_ext/range/each.rb', line 11

def step_with_time_with_zone(n = 1, &block)
  ensure_iteration_allowed
  step_without_time_with_zone(n, &block)
end

#sum(identity = 0) ⇒ Object

Optimize range sum to use arithmetic progression if a block is not given and we have a range of numeric values.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'activesupport/lib/active_support/core_ext/enumerable.rb', line 68

def sum(identity = 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
      (actual_last - first + 1) * (actual_last + first) / 2
    else
      identity
    end
  end
end

#to_formatted_s(format = :default) ⇒ Object Also known as: to_s

Gives a human readable format of the range.

(1..100).to_formatted_s # => "1..100"


9
10
11
12
13
14
15
# File 'activesupport/lib/active_support/core_ext/range/conversions.rb', line 9

def to_formatted_s(format = :default)
  if formatter = RANGE_FORMATS[format]
    formatter.call(first, last)
  else
    to_default_s
  end
end