Class

Range

Inheritance
< Object
Included Modules
Enumerable

A Range represents an interval—a set of values with a start and an end. Ranges may be constructed using the s..e and se literals, or with Range::new. Ranges constructed using .. run from the start to the end inclusively. Those created using exclude the end value. When used as an iterator, ranges return each value in the sequence.

   (-1..-5).to_a      #=> []
   (-5..-1).to_a      #=> [-5, -4, -3, -2, -1]
   ('a'..'e').to_a    #=> ["a", "b", "c", "d", "e"]
   ('a'...'e').to_a   #=> ["a", "b", "c", "d"]

Ranges can be constructed using objects of any type, as long as the objects can be compared using their <=> operator and they support the succ method to return the next object in sequence.

   class Xs                # represent a string of 'x's
     include Comparable
     attr :length
     def initialize(n)
       @length = n
     end
     def succ
       Xs.new(@length + 1)
     end
     def <=>(other)
       @length <=> other.length
     end
     def to_s
       sprintf "%2d #{inspect}", @length
     end
     def inspect
       'x' * @length
     end
   end

   r = Xs.new(3)..Xs.new(6)   #=> xxx..xxxxxx
   r.to_a                     #=> [xxx, xxxx, xxxxx, xxxxxx]
   r.member?(Xs.new(5))       #=> true

In the previous code example, class Xs includes the Comparable module. This is because Enumerable#member? checks for equality using ==. Including Comparable ensures that the == method is defined in terms of the <=> method implemented in Xs.

Methods

Class

Visibility Signature
public new (...)
public yaml_new ( klass, tag, val )

Instance

Visibility Signature
public == (p1)
public === (p1)
public begin ()
public each ()
public end ()
public eql? (p1)
public exclude_end? ()
public first ()
public hash ()
public include? (p1)
public inspect ()
public last ()
public member? (p1)
public pretty_print (q)
public step (...)
public to_s ()
public to_yaml ( opts = {} )

Class Method Detail

Range.new(start, end, exclusive=false) => range

Constructs a range using the given start and end. If the third parameter is omitted or is false, the range will include the end object; otherwise, it will be excluded.

yaml_new( klass, tag, val )

Instance Method Detail

rng == obj => true or false

Returns true only if obj is a Range, has equivalent beginning and end items (by comparing them with ==), and has the same exclude_end? setting as <i>rng</t>.

  (0..2) == (0..2)            #=> true
  (0..2) == Range.new(0,2)    #=> true
  (0..2) == (0...2)           #=> false

rng === obj => true or false
rng.member?(val) => true or false
rng.include?(val) => true or false

Returns true if obj is an element of rng, false otherwise. Conveniently, === is the comparison operator used by case statements.

   case 79
   when 1..50   then   print "low\n"
   when 51..75  then   print "medium\n"
   when 76..100 then   print "high\n"
   end

produces:

   high

rng.first => obj
rng.begin => obj

Returns the first object in rng.

rng.each {| i | block } => rng

Iterates over the elements rng, passing each in turn to the block. You can only iterate if the start object of the range supports the succ method (which means that you can‘t iterate over ranges of Float objects).

   (10..15).each do |n|
      print n, ' '
   end

produces:

   10 11 12 13 14 15

rng.end => obj
rng.last => obj

Returns the object that defines the end of rng.

   (1..10).end    #=> 10
   (1...10).end   #=> 10

rng.eql?(obj) => true or false

Returns true only if obj is a Range, has equivalent beginning and end items (by comparing them with eql?), and has the same exclude_end? setting as rng.

  (0..2) == (0..2)            #=> true
  (0..2) == Range.new(0,2)    #=> true
  (0..2) == (0...2)           #=> false

rng.exclude_end? => true or false

Returns true if rng excludes its end value.

rng.first => obj
rng.begin => obj

Returns the first object in rng.

rng.hash => fixnum

Generate a hash value such that two ranges with the same start and end points, and the same value for the "exclude end" flag, generate the same hash value.

rng === obj => true or false
rng.member?(val) => true or false
rng.include?(val) => true or false

Returns true if obj is an element of rng, false otherwise. Conveniently, === is the comparison operator used by case statements.

   case 79
   when 1..50   then   print "low\n"
   when 51..75  then   print "medium\n"
   when 76..100 then   print "high\n"
   end

produces:

   high

rng.inspect => string

Convert this range object to a printable form (using inspect to convert the start and end objects).

rng.end => obj
rng.last => obj

Returns the object that defines the end of rng.

   (1..10).end    #=> 10
   (1...10).end   #=> 10

rng === obj => true or false
rng.member?(val) => true or false
rng.include?(val) => true or false

Returns true if obj is an element of rng, false otherwise. Conveniently, === is the comparison operator used by case statements.

   case 79
   when 1..50   then   print "low\n"
   when 51..75  then   print "medium\n"
   when 76..100 then   print "high\n"
   end

produces:

   high

pretty_print(q)

rng.step(n=1) {| obj | block } => rng

Iterates over rng, passing each nth element to the block. If the range contains numbers, n is added for each iteration. Otherwise step invokes succ to iterate through range elements. The following code uses class Xs, which is defined in the class-level documentation.

   range = Xs.new(1)..Xs.new(10)
   range.step(2) {|x| puts x}
   range.step(3) {|x| puts x}

produces:

    1 x
    3 xxx
    5 xxxxx
    7 xxxxxxx
    9 xxxxxxxxx
    1 x
    4 xxxx
    7 xxxxxxx
   10 xxxxxxxxxx

rng.to_s => string

Convert this range object to a printable form.

to_yaml( opts = {} )