Class Range
In: lib/pp.rb
lib/yaml/rubytypes.rb

Methods

==   ===   begin   cover?   each   end   eql?   exclude_end?   first   hash   include?   inspect   last   max   member?   min   new   pretty_print   step   to_s   to_yaml   yaml_new  

Included Modules

Enumerable

Public Class methods

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.

Public Instance methods

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

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

Returns the first object in rng.

Returns true if obj is between beg and end, i.e beg <= obj <= end (or end exclusive when exclude_end? is true).

   ("a".."z").cover?("c")    #=> true
   ("a".."z").cover?("5")    #=> false

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

Returns the object that defines the end of rng.

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

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

Returns true if rng excludes its end value.

Returns the first object in rng, or the first n elements.

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.

Returns true if obj is an element of rng, false otherwise. If beg and end are numeric, comparison is done according magnitude of values.

   ("a".."z").include?("g")  # => true
   ("a".."z").include?("A")  # => false

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

Returns the last object in rng, or the last n elements.

Returns the maximum value in rng. The second uses the block to compare values. Returns nil if the first value in range is larger than the last value.

Returns true if obj is an element of rng, false otherwise. If beg and end are numeric, comparison is done according magnitude of values.

   ("a".."z").include?("g")  # => true
   ("a".."z").include?("A")  # => false

Returns the minimum value in rng. The second uses the block to compare values. Returns nil if the first value in range is larger than the last value.

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

Convert this range object to a printable form.

[Validate]

ruby-doc.org is hosted and run by James Britt and Happy Camper Studios, a Ruby application development company in Phoenix, Arizona. Ruby-doc.org was created in 2002 to promote the Ruby language and to help other Ruby hackers.

Documentation content on ruby-doc.org is provided by remarkable members of the Ruby community.

For more information on the Ruby programming language, visit ruby-lang.org.

Want to help improve Ruby's API docs? See Ruby Documentation Guidelines.