Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Ruby—A Diamond of a Programming Language, Part 2 : Page 4

Get ready to dive deeper into the power and elegance of the language lauded as a possible contender to replace current programming languages.


advertisement
Ranges
A very unusual but useful definition in Ruby is the concept of a range. A range is simply a set of sequential values. For example, the characters a through z define a range inclusive of all the lowercase characters in the English alphabet. Another example is the range of integers 1 through 10. A range can be created from any type of object given the type of object allows comparison using the Ruby comparison operator (<=>) and the succ method. The <=> operator returns -1, 0 or +1 depending on whether the left-hand operand is less than, equal to or greater than the right-hand operand. For example "A"<=>"B" both return -1. The succ method runs on the integer 4 (4.succ) would return 5 and succ run on a returns b.

Ranges can be created using the new method on the Range class or using special notation. Below, two identical ranges for uppercase letters are created in irb using parentheses and dot-dot shorthand notation.

irb(main):001:0> r1=Range.new("A","Z") => "A".."Z" irb(main):002:0> r2=("A".."Z") => "A".."Z"

When creating a range, a beginning and ending value must be specified; in this case A as the beginning value and Z as the ending value. When creating a range, you can also signify whether the range should be inclusive or exclusive of the end element. By default, as shown above, the range includes the end value. To exclude the end element from the range, use the exclusive parameter on the new method or triple-dot shorthand notation as shown below.


irb(main):001:0> r1=Range.new("A","Z",true) => "A"..."Z" irb(main):002:0> r2=("A"..."Z") => "A"..."Z" irb(main):003:0> r1.include?"Z" => false irb(main):004:0> r2.include?"Z" => false

The include? method called on a range as shown above indicates whether the parameter is a member of the range. In the case shown, “Z” is not an element. There is an operator for this same method. “===” (that’s three equal signs together) performs the same task.

irb(main):005:0> r2==="Z" =≫ false

Ranges are used in many aspects of Ruby programming. In particular, they have two specific uses: as generators and predicates. As a generator, the method each on a range allows you to iterate through each element in the range. For example, say you wanted to determine the number of actual bytes in a range of kilobytes. The code shown run in the irb below uses a range as generator for kilobytes to bytes.

irb(main):008:0> kilobytes=(1..10) kilobytes.each{|x| puts x*1024} => 1..10 1024 2048 3072 4096 5120 6144 7168 8192 9216 10240

In Ruby conditional logic ranges can be used as predicates, usually with the help of the === operator. For example, you could use a range predicates to test an integer reference against valid port numbers (0 through 65535) and reserved ports (0 through 1024 non-inclusive).

irb(main):001:0> proposedPort = 8080 validPorts=(0..65535) reservedPorts=(0...1024) if (validPorts === proposedPort) & !(reservedPorts === proposedPort) puts "Proposed port is ok to use." else puts "Proposed port is not allowed to be used." end => 8080 => 0..65535 => 0...1024 Proposed port is ok to use.

Ranges are also very useful in accessing elements of data structures like arrays and hashes which is our next topic.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap