Range of hex values?

Discussion in 'Ruby' started by Ammar Ali, Oct 30, 2010.

  1. Ammar Ali

    Ammar Ali Guest

    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
     
    Ammar Ali, Oct 30, 2010
    #1
    1. Advertising

  2. Ammar Ali

    ara.t.howard Guest

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

    On Oct 30, 3:33=A0am, Ammar Ali <> wrote:
    > Hello,
    >
    > In 1.8.[67] the following range does not produce what I expected:
    >
    > =A0 ("\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:
    >
    > =A0 (0..127).to_a.map {|c| c.chr}
    >
    > Regards,
    > Ammar
     
    ara.t.howard, Oct 30, 2010
    #2
    1. Advertising

  3. Ammar Ali

    Ammar Ali Guest

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

    On Sat, Oct 30, 2010 at 4:33 PM, ara.t.howard <>wrote:

    > 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
     
    Ammar Ali, Oct 30, 2010
    #3
  4. On 30.10.2010 11:33, Ammar Ali wrote:
    > 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

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Oct 31, 2010
    #4
  5. Ammar Ali

    Ammar Ali Guest

    On Sun, Oct 31, 2010 at 2:45 PM, Robert Klemme
    <> wrote:
    >
    > On 30.10.2010 11:33, Ammar Ali wrote:
    >>
    >> A workaround that does the right thing in all versions:
    >>
    >> =C2=A0 (0..127).to_a.map {|c| c.chr}

    >
    > 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
     
    Ammar Ali, Oct 31, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,229
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    474
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    590
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,892
  5. tim
    Replies:
    2
    Views:
    1,575
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page