Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Ruby Programming
Previous Page Home Next Page
class Range
Parent: Object
Version: 1.6


new === begin each end exclude_end? first last length size

A Range represents an interval---a set of values with a start and an end. Ranges may be constructed using the s .. e and s ... e literals, or with . 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
  def succ + 1)
  def <=>(other)
    raise TypeError unless other.kind_of? Xs
    @length <=> other.length
  def inspect
    'x' * @length
r = xxx..xxxxxx
r.to_a [xxx, xxxx, xxxxx, xxxxxx]
r.member?( true

Enumerable: collect, detect, each_with_index, entries, find, find_all, grep, include?, map, max, member?, min, reject, select, sort, to_a

class methods
new start, end, exclusive =false ) -> aRange

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.

instance methods
=== rng === anObject -> true or false

Returns true if anObject 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"

begin rng.begin -> anObject

Returns the first object of rng.

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

Iterates over the elements rng, passing each in turn to the block.

(10..15).each do |n|
   print n, ' '
10 11 12 13 14 15

end rng.end -> anObject

Returns the object that defines the end of rng. See also Range#length .

(1..10).end 10
(1...10).end 10

exclude_end? rng.exclude_end? -> true or false

Returns true if rng excludes its end value.

first rng.first -> anObject

Returns the first object in rng.

last rng.last -> anObject

Synonym for Range#end .

length rng.length -> anInteger

Returns the number of objects in rng.

(1..10).length 10
(1...10).length 9

size rng.size -> anInteger

Synonym for Range#length .

Ruby Programming
Previous Page Home Next Page

  Published under the terms of the Open Publication License Design by Interspire