A Range
object
represents the values
between a start value and an end value. Range literals are written by
placing two or three dots between the start and end value. If two dots are used, then the range is inclusive and the end value is part
of the range. If three dots are used, then the range is exclusive and the end value is not
part of the range:
1..10 # The integers 1 through 10, including 10 1.0...10.0 # The numbers between 1.0 and 10.0, excluding 10.0 itself
Test whether a value is included in a range with the include?
method (but see below for a discussion of alternatives):
cold_war = 1945..1989 cold_war.include? birthdate.year
Implicit in the definition of a range is the notion of ordering.
If a range is the values between two endpoints, there obviously must be
some way to compare values to those endpoints. In Ruby, this is done
with the comparison operator <=>
,
which compares its two operands and evaluates to –1
, 0
, or
1
, depending on their relative order
(or equality). Classes such as numbers and strings that have an ordering
define the <=>
operator. A
value can only be used as a range endpoint if it responds to this
operator. The endpoints of a range and the values “in” the range are
typically all of the same class. Technically, however, any value that is
compatible with the <=>
operators of the range endpoints can be considered a member of the
range.
The primary purpose for ranges is comparison: to be able to
determine whether a value is in or out of the range. An important
secondary purpose is iteration: if the class of the endpoints of a range
defines a succ
method (for
successor), then there is a discrete set of range members, and they can
be iterated with each
, step
, and Enumerable
methods. Consider the range
'a'..'c'
, for example:
r = 'a'..'c' r.each {|l| print "[#{l}]"} # Prints "[a][b][c]" r.step(2) { |l| print "[#{l}]"} # Prints "[a][c]" r.to_a # => ['a','b','c']: Enumerable defines to_a
The reason this works is that the String
class defines a succ
method and 'a'.succ
is 'b'
and 'b'.succ
is 'c'
. Ranges that can be iterated like this
are discrete ranges. Ranges whose
endpoints do not define a succ
method
cannot be iterated, and so they can be called continuous. Note that ranges with
integer endpoints are discrete, but floating-point numbers as endpoints
are continuous.
Ranges with integer endpoints are the most commonly used in typical Ruby programs. Because they are discrete, integer ranges can be used to index strings and arrays. They are also a convenient way to represent an enumerable collection of ascending values.
Notice that the code assigns a range literal to a variable, and
then invokes methods on the range through the variable. If you want to
invoke a method directly on a range literal, you must parenthesize the
literal, or the method invocation is actually on the endpoint of the
range rather than on the Range
object
itself:
1..3.to_a # Tries to call to_a on the number 3 (1..3).to_a # => [1,2,3]
The Range
class defines methods for determining whether an arbitrary
value is a member of (i.e., is included in) a range. Before going into
detail on these methods, it is necessary to explain that range
membership can be defined in two different ways that are related to
the difference between continuous and discrete ranges. A value
x
is a member of the range begin..end
by the first definition
if:
begin <= x <= end
And x
is a member of the
range begin...end
(with three dots)
if:
begin <= x < end
All range endpoint values must implement the <=>
operator, so this definition of membership works for any Range
object and does not require the
endpoints to implement the succ
method. We’ll call this the continuous membership test.
The second definition of membership—discrete
membership—does depend on succ
. It
treats a Range
begin..end
as a set that includes begin
, begin.succ
, begin.succ.succ
, and so on. By this
definition, range membership is set membership, and a value x
is included in a range only if it is a
value returned by one of the succ
invocations. Note that testing for discrete membership is potentially
a much more expensive operation
than testing for continuous membership.
With that as background, we can describe the Range
methods for testing membership. Ruby
1.8 supports two methods, include?
and member?
. They are synonyms, and both use the continuous membership
test:
r = 0...100 # The range of integers 0 through 99 r.member? 50 # => true: 50 is a member of the range r.include? 100 # => false: 100 is excluded from the range r.include? 99.9 # => true: 99.9 is less than 100
The situation is different in Ruby 1.9. That version of the
language introduces a new method, cover?
, which
works like include?
and member?
do in Ruby 1.8: it always uses the
continuous membership test. include?
and member?
are still synonyms in Ruby 1.9. If
the endpoints of the range are numbers, these methods use the
continuous membership test, just as they did in Ruby 1.8. If the
endpoints are not numeric, however, they instead use the discrete
membership test. We can illustrate these changes with a discrete range
of strings (you may want to use ri to understand
how String.succ
works):
triples = "AAA".."ZZZ" triples.include? "ABC" # true; fast in 1.8 and slow in 1.9 triples.include? "ABCD" # true in 1.8, false in 1.9 triples.cover? "ABCD" # true and fast in 1.9 triples.to_a.include? "ABCD" # false and slow in 1.8 and 1.9
In practice, most ranges have numeric endpoints, and the
Range
API changes between Ruby 1.8
and 1.9 have little impact.
3.133.116.137