July 2011
Intermediate to advanced
400 pages
8h 46m
English
In Ruby, a Range is a class that represents a set of values defined by a starting value and an ending value. Typically a range is defined using integers, but it may also be defined using other ordered values such as floating-point numbers or characters. Values can be negative, though you should be careful that your starting value is lower than your ending value!
Here are a few examples:
ranges.rb
a = (1..10)
b = (-10..-1)
c = (-10..10)
d = ('a'..'z')You can also specify ranges using three dots instead of two; this creates a range that omits the final value:
d = ('a'..'z') # this two-dot range = 'a'..'z'
e = ('a'...'z') # this three-dot range = 'a'..'y'You can create an array of the values defined by a range using the to_a method, like this: ...
Read now
Unlock full access