Range of hex values?

A

Ammar Ali

Hello,

In 1.8.[67] the following range does not produce what I expected:

("\x00".."\x7F").to_a

It only produces the characters between \x00 and \x39, while in
1.9.[12] it produces the full expected range.
I'm curious if this would be considered a bug, intentional, or I'm
missing something?

A workaround that does the right thing in all versions:

(0..127).to_a.map {|c| c.chr}

Regards,
Ammar
 
A

ara.t.howard

you are relying on String#succ! there. read the docs to understand
why it is not what you want.
 
A

Ammar Ali

[Note: parts of this message were removed to make it a legal post.]

you are relying on String#succ! there. read the docs to understand
why it is not what you want.


Thanks for the tip. I read the docs and it did shed some light on what is
going on behind the scenes, but I still don't get why it works in 1.9. I
will have to investigate further.

Thanks again,
Ammar
 
R

Robert Klemme

In 1.8.[67] the following range does not produce what I expected:

("\x00".."\x7F").to_a

It only produces the characters between \x00 and \x39, while in
1.9.[12] it produces the full expected range.
I'm curious if this would be considered a bug, intentional, or I'm
missing something?

A workaround that does the right thing in all versions:

(0..127).to_a.map {|c| c.chr}

The ".to_a" is superfluous here. Here are other options

127.times.map &:chr
(0..127).map &:chr

Kind regards

robert
 
A

Ammar Ali

The ".to_a" is superfluous here. =C2=A0Here are other options

127.times.map &:chr
(0..127).map &:chr


Thanks Robert. Another great example of the "TIMTOWTDI-ness" of ruby
and how one's view gets shaped by where they started from.

I have a slight preference for the one that includes the range. It
tells me just a little bit more about what the intention is. Obviously
that's just a matter of taste, or lack thereof.

I still haven't figured out why it works under 1.9. I guess
String#succ has changed.

Cheers,
Ammar
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top