Ruby Ranges
Previous | Table of Contents | Next |
Ruby Methods | Understanding Ruby Arrays |
Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99 |
Ruby Ranges allow data to be represented in the form of a range (in other words a data set with start and end values and a logical sequence of values in between). The values in a range can be numbers, characters, strings or objects. In this chapter we will look at the three types of range supported by Ruby, namely sequences, conditions and intervals.
Ruby Sequence Ranges
Sequence ranges in Ruby are used to create a range of successive values - consisting of a start value, an end value and a range of values in between.
Two operators are available for creating such ranges, the inclusive two-dot (..) operator and the exclusive three-dot operator (...). The inclusive operator includes both the first and last values in the range. The exclusive range operator excludes the last value from the sequence. For example:
1..10 # Creates a range from 1 to 10 inclusive 1...10 # Creates a range from 1 to 9
A range may be converted to an array using the Ruby to_a method. For example:
(1..10).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] (1...10).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9]
As mentioned previously, ranges are not restricted to numerical values. We can also create a character based range:
('a'..'l').to_a => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]
Also, we can create ranges based on string values:
('cab'..'car').to_a => ["cab", "cac", "cad", "cae", "caf", "cag", "cah", "cai", "caj", "cak", "cal", "cam", "can", "cao", "cap", "caq", "car"]
Range values may also be objects. The only requirements for placing an object in a range is that the object provides a mechanism for returning the next object in the sequence via succ and it must be possible to compare the objects using the <=> operator.
Using Range Methods
Given the object-oriented nature of Ruby it should come as no surprise to you that a range is actually an object. As such, there are a number of methods of the Range class which can be accessed when working with a range object.
words = 'cab'..'car' words.min # get lowest value in range => "cab" words.max # get highest value in range => "car" words.include?('can') # check to see if a value exists in the range => true words.reject {|subrange| subrange < 'cal'} # reject values below a specified range value => ["cal", "cam", "can", "cao", "cap", "caq", "car"] words.each {|word| puts "Hello " + word} # iterate through each value and perform a task Hello cab Hello cac Hello cad Hello cae Hello caf Hello cag Hello cah Hello cai Hello caj Hello cak Hello cal Hello cam Hello can Hello cao Hello cap Hello caq Hello car
Ruby Ranges as Conditional Expressions
while input = gets puts input + " triggered" if input =~ /start/ .. input =~ /end/ end
Ruby Range Intervals
Ranges are ideal for identifying if a value falls within a particular range. For example, we might want to know whether a number is within a certain range, or a character within a certain group of letters arranged in alphabetical order. This is achieved using the === equality operator:
(1..20) === 15 # Does the number fit in the range 1 to 20 => true ('k'..'z') === 'm' # Does the letter fall between the letters 'k' and 'z' in the alphabet? => true
Ranges in case Statements
Ranges are perhaps at their most powerful when they are used in conjunction with a case statement (for more information also see The Ruby case Statement chapter):
score = 70 result = case score when 0..40: "Fail" when 41..60: "Pass" when 61..70: "Pass with Merit" when 71..100: "Pass with Distinction" else "Invalid Score" end puts result
Purchase and download the full PDF and ePub editions of this Ruby eBook for only $8.99 |
Previous | Table of Contents | Next |
Ruby Methods | Understanding Ruby Arrays |