Is there another method like .next?

M

Michael W. Ryder

I was looking for a way to iterate through the ascii values and was
trying to get it to not go from "9" to "10" or "Z" to "AA". Evidently
the string method .next always returns digits for digits and letters for
letters. I could do something like:

a = "9"
b = ""
b << a[0].next => "["

but this seems overly complicated. I have to initialize b or Ruby
complains of an undefined local variable or method and other methods of
string assignment return "91".
 
T

Todd Benson

I was looking for a way to iterate through the ascii values

For classic printables...

(32...128).map {|int| int.chr}

You then have an array which you can #join if you want.

Todd
 
M

Michael W. Ryder

Todd said:
For classic printables...

(32...128).map {|int| int.chr}
I think this should be (32...127).map as 128 is the not printable.
I had forgotten about .chr. This allows me to do b = a[0].next.chr and
get the desired result. This is a little more than I expected but a lot
simpler than my first attempt. Thanks for the pointer.
 
T

Todd Benson

I think this should be (32...127).map as 128 is the not printable.

Thanks for the correction :) Actually, it's the 127 that's not
printable, but who's counting.

Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,616
Latest member
gijoji4272

Latest Threads

Top