Strings and ranges

“David A. Black”
<dblack@wobblini To: ruby-talk@ruby-lang.org (ruby-talk ML)
.net> cc:
12/03/2003 10:48
AM
Please respond
to ruby-talk

Hi –

The following program doesn’t execute as I would expect:

#!/usr/bin/ruby

stringy = “X”

1.upto(10) do |i|
puts stringy
stringy.succ!
end

“X”.upto(“AG”) do |ss|
puts ss
end

puts “‘AB’ > ‘AA’ = #{‘AB’ > ‘AA’}”
puts “‘X’ < ‘AA’ = #{‘X’ < ‘AB’}”

Interesting – just like “9” and “10” (i.e., “9” > “9”.succ).

The code below gets around my problem…

stringy = “X”
while true
puts stringy
stringy.succ!
break if stringy == “AH”
end

Is there a better way?

How about this:

x = “X”
puts (0…10).map { x.succ! .dup }

(You need the .dup or you’ll end up with 10 of x, all the same
string.)

···

Subject: Re: strings and ranges
On Thu, 4 Dec 2003 charlie.mills@milliman.com wrote:


That works.
I guess I also want to capture the ability to iterate until you reach a
given string.
So the problem with going through the range 0…10 is you have to have a
way of mapping “X” to 0 and “AG” to 10 (or to two numbers whose difference
is 10).
(I guess you also have to know that “Z”.succ → “AA”, and that “X” and “AG”
are of the form /[A-Z]+/)

-Charlie


This communication is intended solely for the addressee and is
confidential. If you are not the intended recipient, any disclosure,
copying, distribution or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. Unless indicated
to the contrary: it does not constitute professional advice or
opinions upon which reliance may be made by the addressee or any
other party, and it should be considered to be a work in progress.