Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #7259
| From | Christian Schweingruber <chrigu@lorraine.ch> |
|---|---|
| Newsgroups | comp.lang.ruby |
| Subject | Re: A question about range operator with string operands |
| Date | 2016-06-02 15:27 +0200 |
| Organization | albasani.net |
| Message-ID | <nipc81$e6c$1@news.albasani.net> (permalink) |
| References | <3a5be55a-18b8-45fd-83ad-ec6dac81d299@googlegroups.com> |
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.
>
Back to comp.lang.ruby | Previous | Next — Previous in thread | Next in thread | Find similar
A question about range operator with string operands Naotaka Hirano <n-hirano@computex.co.jp> - 2016-06-01 23:14 -0700
Re: A question about range operator with string operands Christian Schweingruber <chrigu@lorraine.ch> - 2016-06-02 15:27 +0200
Re: A question about range operator with string operands Naotaka Hirano <n-hirano@computex.co.jp> - 2016-06-03 00:24 -0700
csiph-web