f



A question about range operator with string operands

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.
0
Naotaka
6/2/2016 6:14:38 AM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

2 Replies
376 Views

Similar Articles

[PageSpeed] 5

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.
> 

0
Christian
6/2/2016 1:27:28 PM
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


0
Naotaka
6/3/2016 7:24:14 AM
Reply: