• A question about range operator with string operands

    From Naotaka Hirano@21:1/5 to All on Wed Jun 1 23:14:38 2016
    Hello, everyone.

    I hava a question about range operator with string operands.

    =================================================================================
    irb(main):001:0> ("A9".."A_").to_a
    ["A9", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "C0", "C1",
    <<partially omitted>>
    "Y8", "Y9", "Z0", "Z1", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9"] irb(main):002:0> ("B0".."A_").to_a
    []
    =================================================================================

    I feel it doesn't make sense.
    I mean, I don't know why ("A9".."A_").to_a includes items from "B0" to "Z9". So, could someone tell me what chapter/section I should read in the reference manual or other documents?

    Thank you.

    note : ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
    on ubuntu 14.04 LTS.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Schweingruber@21:1/5 to All on Thu Jun 2 15:27:28 2016
    Hi

    Am 02.06.2016 um 08:14 schrieb Naotaka Hirano:
    Hello, everyone.

    I hava a question about range operator with string operands.

    =================================================================================
    irb(main):001:0> ("A9".."A_").to_a
    ["A9", "B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9", "C0", "C1",
    <<partially omitted>>
    "Y8", "Y9", "Z0", "Z1", "Z2", "Z3", "Z4", "Z5", "Z6", "Z7", "Z8", "Z9"] irb(main):002:0> ("B0".."A_").to_a
    []
    =================================================================================

    the range operator is using the upto methode
    "A9".upto("A_").to_a is the same as ("A9".."A_").to_a

    http://apidock.com/ruby/v1_9_3_392/String/upto

    and upto is using the succ methode:
    http://apidock.com/ruby/String/succ
    Returns the successor to str. The successor is calculated by incrementing characters starting from the rightmost
    alphanumeric (or the rightmost character if there are no alphanumerics) in the string. Incrementing a digit always
    results in another digit, and incrementing a letter results in another letter of the same case. Incrementing
    nonalphanumerics uses the underlying character set’s collating sequence.

    If the increment generates a “carry,” the character to the left of it is incremented. This process repeats until there
    is no carry, adding an additional character if necessary.

    "A9".succ is "B0"
    "Z9".succ is "AA0" (upto stops here because stringlength has grown)

    not very logical, but this explains these results!

    greetings
    Chrigu


    I feel it doesn't make sense.
    I mean, I don't know why ("A9".."A_").to_a includes items from "B0" to "Z9". So, could someone tell me what chapter/section I should read in the reference manual or other documents?

    Thank you.

    note : ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
    on ubuntu 14.04 LTS.


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Naotaka Hirano@21:1/5 to All on Fri Jun 3 00:24:14 2016
    Hello, Mr. Christian Schweingruber,

    Thank you for your help.

    I haven't yet reached a complete understanding of String.upto's behavior, especially at End conditions, but I feel my sight is getting clear.

    Anyhow, to avoid confusion and as a quick solution,
    I think I usually shouldn't use such irregular operands.

    I'll read the documents more carefully. Maybe the source code too.

    Regards,
    Naotaka

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)