Section 2.34. Calculating a 32-Bit CRC

2.33. Generating Successive Strings

On rare occasions we may want to find the "successor" value for a string; for example, the successor for "aaa" is "aab" (then "aad", "aae", and so on).

Ruby provides the method succ for this purpose:

droid = "R2D2" improved  = droid.succ         # "R2D3" pill  = "Vitamin B" pill2 = pill.succ              # "Vitamin C"

We don't recommend the use of this feature unless the values are predictable and reasonable. If you start with a string that is esoteric enough, you will eventually get strange and surprising results.

There is also an upto method that applies succ repeatedly in a loop until the desired final value is reached:

"Files, A".upto "Files, X" do |letter|   puts "Opening: #{letter}" end # Produces 24 lines of output

Again, we stress that this is not used frequently, and you use it at your own risk. Also we want to point out that there is no corresponding "predecessor" function at the time of this writing.

The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: