Bug #13663
closed`String#upto` doesn't work as expected
Description
Given that String#upto
uses String#succ
to generate successive values, I'd expect
'x'.upto('ac').to_a #=> []
to return:
["x", "y", "z", "aa", "ab", "ac"]
Instead, an empty array is returned.
This seems to depend on whether the the receiver is greater than the argument or not:
'x' <=> 'ac' #=> 1
It works just fine in this case:
'b'.upto('ca').to_a
#=> ["b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
# "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "aa",
# "ab", "ac", "ad", "ae", "af", "ag", "ah", "ai", "aj", "ak", "al",
# "am", "an", "ao", "ap", "aq", "ar", "as", "at", "au", "av", "aw",
# "ax", "ay", "az", "ba", "bb", "bc", "bd", "be", "bf", "bg", "bh",
# "bi", "bj", "bk", "bl", "bm", "bn", "bo", "bp", "bq", "br", "bs",
# "bt", "bu", "bv", "bw", "bx", "by", "bz", "ca"]
Presumably because of:
'b' <=> 'ca' #=> -1
Updated by zverok (Victor Shepelev) over 7 years ago
I believe that problem here is how to provide consistency between succ
and <=>
for arbitrary length strings.
- For most of the real use cases,
'x' > 'ac'
is sane (like sorting strings); - Things using
succ
(likeupto
and ranges) should check that begin is lower than end;
So... I believe that it is only reasonable to have (1) and (2), though sometimes it leads to "inconsistencies", like described above. If you do a lot of stuff with making ranges from "x" to "ac" it is probably better to have dedicated value object class, with redefined <=>
and succ
Updated by Hanmac (Hans Mackowiak) over 7 years ago
Also i think its a bit to much optimised.
because i tried to overwrite the String#<=> method, directly or with Refinements, and it didnt work as i want it to.
what works is using my own Value Object Class.
Updated by sos4nt (Stefan Schüßler) over 7 years ago
I'm not asking for consistency between String#succ
and String#<=>
(although that would be desirable). I do understand that the discrepancy may result in surprising results when generating sequences via Range
because Range
relies on these methods. Range
checks via <=>
if the value generated by succ
is indeed a successor.
But String#upto
doesn't have the limitations Range
has. It doesn't have to work with arbitrary objects and it doesn't have to depend on <=>
. It can apply whatever logic is needed to ensure a sane result.
Updated by jeremyevans0 (Jeremy Evans) about 4 years ago
- Status changed from Open to Closed
At the December 2020 development meeting, @matz (Yukihiro Matsumoto) agreed the current behavior is expected and this is not a bug.
Updated by mame (Yusuke Endoh) about 4 years ago
- Is duplicate of Feature #2323: "Z".."Z".succが空 added
Updated by mame (Yusuke Endoh) about 4 years ago
- Is duplicate of Feature #5607: Inconsistent reaction in Range of String added