* [ruby-core:81690] [Ruby trunk Bug#13663] `String#upto` doesn't work as expected
[not found] <redmine.issue-13663.20170615072454@ruby-lang.org>
@ 2017-06-15 7:24 ` mail
2017-06-15 12:53 ` [ruby-core:81691] " zverok.offline
` (2 subsequent siblings)
3 siblings, 0 replies; 4+ messages in thread
From: mail @ 2017-06-15 7:24 UTC (permalink / raw)
To: ruby-core
Issue #13663 has been reported by sos4nt (Stefan Schüßler).
----------------------------------------
Bug #13663: `String#upto` doesn't work as expected
https://bugs.ruby-lang.org/issues/13663
* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Given that `String#upto` uses `String#succ` to generate successive values, I'd expect
```ruby
'x'.upto('ac').to_a #=> []
```
to return:
```ruby
["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:
```ruby
'x' <=> 'ac' #=> 1
```
It works just fine in this case:
```ruby
'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:
```ruby
'b' <=> 'ca' #=> -1
```
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
* [ruby-core:81691] [Ruby trunk Bug#13663] `String#upto` doesn't work as expected
[not found] <redmine.issue-13663.20170615072454@ruby-lang.org>
2017-06-15 7:24 ` [ruby-core:81690] [Ruby trunk Bug#13663] `String#upto` doesn't work as expected mail
@ 2017-06-15 12:53 ` zverok.offline
2017-06-15 13:08 ` [ruby-core:81692] " hanmac
2017-06-23 11:47 ` [ruby-core:81750] " mail
3 siblings, 0 replies; 4+ messages in thread
From: zverok.offline @ 2017-06-15 12:53 UTC (permalink / raw)
To: ruby-core
Issue #13663 has been updated by zverok (Victor Shepelev).
I believe that problem here is how to provide consistency between `succ` and `<=>` for arbitrary length strings.
1. For most of the real use cases, `'x' > 'ac'` is sane (like sorting strings);
2. Things using `succ` (like `upto` 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`
----------------------------------------
Bug #13663: `String#upto` doesn't work as expected
https://bugs.ruby-lang.org/issues/13663#change-65380
* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Given that `String#upto` uses `String#succ` to generate successive values, I'd expect
```ruby
'x'.upto('ac').to_a #=> []
```
to return:
```ruby
["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:
```ruby
'x' <=> 'ac' #=> 1
```
It works just fine in this case:
```ruby
'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:
```ruby
'b' <=> 'ca' #=> -1
```
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
* [ruby-core:81692] [Ruby trunk Bug#13663] `String#upto` doesn't work as expected
[not found] <redmine.issue-13663.20170615072454@ruby-lang.org>
2017-06-15 7:24 ` [ruby-core:81690] [Ruby trunk Bug#13663] `String#upto` doesn't work as expected mail
2017-06-15 12:53 ` [ruby-core:81691] " zverok.offline
@ 2017-06-15 13:08 ` hanmac
2017-06-23 11:47 ` [ruby-core:81750] " mail
3 siblings, 0 replies; 4+ messages in thread
From: hanmac @ 2017-06-15 13:08 UTC (permalink / raw)
To: ruby-core
Issue #13663 has been updated by Hanmac (Hans Mackowiak).
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.
----------------------------------------
Bug #13663: `String#upto` doesn't work as expected
https://bugs.ruby-lang.org/issues/13663#change-65381
* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Given that `String#upto` uses `String#succ` to generate successive values, I'd expect
```ruby
'x'.upto('ac').to_a #=> []
```
to return:
```ruby
["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:
```ruby
'x' <=> 'ac' #=> 1
```
It works just fine in this case:
```ruby
'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:
```ruby
'b' <=> 'ca' #=> -1
```
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
* [ruby-core:81750] [Ruby trunk Bug#13663] `String#upto` doesn't work as expected
[not found] <redmine.issue-13663.20170615072454@ruby-lang.org>
` (2 preceding siblings ...)
2017-06-15 13:08 ` [ruby-core:81692] " hanmac
@ 2017-06-23 11:47 ` mail
3 siblings, 0 replies; 4+ messages in thread
From: mail @ 2017-06-23 11:47 UTC (permalink / raw)
To: ruby-core
Issue #13663 has been updated by sos4nt (Stefan Schüßler).
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.
----------------------------------------
Bug #13663: `String#upto` doesn't work as expected
https://bugs.ruby-lang.org/issues/13663#change-65450
* Author: sos4nt (Stefan Schüßler)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Given that `String#upto` uses `String#succ` to generate successive values, I'd expect
```ruby
'x'.upto('ac').to_a #=> []
```
to return:
```ruby
["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:
```ruby
'x' <=> 'ac' #=> 1
```
It works just fine in this case:
```ruby
'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:
```ruby
'b' <=> 'ca' #=> -1
```
--
https://bugs.ruby-lang.org/
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2017-06-23 11:47 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <redmine.issue-13663.20170615072454@ruby-lang.org>
2017-06-15 7:24 ` [ruby-core:81690] [Ruby trunk Bug#13663] `String#upto` doesn't work as expected mail
2017-06-15 12:53 ` [ruby-core:81691] " zverok.offline
2017-06-15 13:08 ` [ruby-core:81692] " hanmac
2017-06-23 11:47 ` [ruby-core:81750] " mail
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).