class Range[T]
include Enumerable[T]
The values from begin to end: 1..3 includes 3, 1...3 excludes
it. Either bound may be left out: (1..) is endless and (..3)
beginless; their missing bound is nil.
Integer ranges iterate by 1 and String ranges by String#succ, as
String#upto does; a Float range has no elements to iterate over
(#each and #to_a raise TypeError) but #include? works. Only an
Int range has #size.
An endless integer range iterates until the block breaks, or up to the
largest Int (9223372036854775807), where integer loops stop rather
than overflow; #to_a on it raises TypeError. A range is Enumerable, so (1..3).map { } and
(1..).first(n) work.
A range whose start comes after its end is empty: 3..1, and a
String range whose start sorts after its end ("c".."a",
"b".."aa"); a String range steps as String#upto does, so
("az".."b").to_a is ["az"] and ("9".."10").to_a is ["9", "10"].
(1..3).to_a # => [1, 2, 3]
(1...3).to_a # => [1, 2]
("c".."a").to_a # => []
("c".."a").empty? # => true
(1..).first(2) # => [1, 2]
(1.0..2.0).include?(1.5) # => true
(1..3).sum # => 6
<prelude>:4987
Methods
def each(&: (T) -> Void) -> Void
Calls the block with each value in order.
Raises
TypeError for a beginless range or a range that is neither of Int
nor of String (a Float range).
<prelude>:4997
def begin -> T?
The first bound, or nil for a beginless range.
<prelude>:4999
def end -> T?
The last bound (included or not), or nil for an endless range.
<prelude>:5001
def first -> T
The first bound: #begin, as Ruby's, without the nil (#begin gives
nil for a beginless range). It is returned even when the range is
empty ((3..1).first is 3).
Raises
RangeError for a beginless range.
<prelude>:5009
def last -> T
The last bound: #end, as Ruby's, included or not ((1...3).last is
3, (1...1).last is 1), without the nil (#end gives nil for an
endless range); #max is the largest value a range holds.
(1..3).last # => 3
(1...3).last # => 3
Raises
RangeError for an endless range.
<prelude>:5022
def exclude_end? -> Bool
Whether the range leaves out its end (1...3).
<prelude>:5024
def include?(value: Object) -> Bool
Whether value lies between the bounds, by comparison, not by
iteration: (1..3).include?(2.5) and ("a".."c").include?("bb") hold
(Ruby iterates a String range here). A value of another type gives
false.
(1...3).include?(3) # => false
(1..).include?(1000) # => true
<prelude>:5034
def cover?(value: T) -> Bool
Whether value lies between the bounds, by comparison: #include? for
a value of the element type.
<prelude>:5037
def empty? -> Bool
Whether the range has no value. False for an endless or beginless range.
Raises
<prelude>:5044
def step(step: Int, &: (T) -> Void) -> Void
Calls the block with every step-th value of an integer range, from
begin.
Raises
ArgumentError when step is not positive (Ruby counts down with a
negative step); TypeError when the range does not start at an
integer and end at one or at nothing (endless).
(1..10).step(3).to_a # => [1, 4, 7, 10]
(10..1).step(-3).to_a # raises ArgumentError
<prelude>:5058
def step(step: Int) -> Enumerator[T]
An enumerator of every step-th value.
The enumerator is made whatever step and the range are: a step
that is not positive or a range that does not start at an integer
and end at one or at nothing raises, as step(step) { } does, only
when the enumerator is iterated (ArgumentError or TypeError).
e = (1..10).step(0)
e.to_a # raises ArgumentError
<prelude>:5070
def each -> Enumerator[T]
An enumerator over the values.
<prelude>:5072
def reverse_each(&: (T) -> Void) -> Void
Calls the block with each value, last first.
Raises
TypeError for an endless range, as #each does.
<prelude>:5078
def to_a -> Array[T]
The values as an array.
Raises
TypeError for an endless or beginless range or a Float range.
<prelude>:5084
def to_s -> String
The range with its bounds' to_s, a missing one shown as nothing.
(1...3).to_s # => "1...3"
("a".."b").to_s # => "a..b"
(1..).to_s # => "1.."
<prelude>:5092
def inspect -> String
The range with its bounds' inspect. A missing bound is left out,
unless both are.
("a".."b").inspect # => "\"a\"..\"b\""
(1..).inspect # => "1.."
(..3).inspect # => "..3"
(nil..nil).inspect # => "nil..nil"
<prelude>:5102
def ==(other: Object) -> Bool
Whether other is a range with == bounds that excludes its end just
as this one does.
<prelude>:5105
def hash -> Int
A hash code: ranges that are == have the same one.
<prelude>:5107
def last(n: Int) -> Array[T]
The last n values (all of them when there are fewer).
Raises
RangeError for an endless range, TypeError when the range cannot
be iterated over otherwise (as #to_a), ArgumentError for a
negative n.
<prelude>:5115
def size -> Int
The number of values; 0 when the range is empty.
Raises
TypeError for an endless or beginless range.
(1..10).size # => 10
(1...10).size # => 9
(3..1).size # => 0
<prelude>:5136