Issue #5607 has been updated by Benoit Daloze.


Hi,

This is indeed surprising.
Range#to_a is calling Range#each which has a special case for Strings to call String#upto, which is said to use String#succ.

However, in rb_str_upto (string.c:2995), there is a test that do not yield correspondingly to #succ mentioned in the documentation:

    n = rb_str_cmp(beg, end);
    if (n > 0 || (excl && n == 0)) return beg;

In your case "X" <=> "AB" returns 1, so nothing is yielded.
The assumption to yield nothing when beg > end is not producing an intuitive result in this case, because the definition of <=> is using a different comparison and so a <=> a.succ might as well be -1 or 1.

I believe this test should be changed to use a String#succ -based comparison, if this is possible.

P.S.: The documentation starts with:
Iterates through successive values, starting at <i>str</i> and
ending at <i>other_str</i> inclusive, [...]

I believe "inclusive" should be removed there, as it depends whether the exclusive option is set and is explained further.

P.S.2: I'm not sure it is right to use different methods (not only #succ) in Range#each while being undocumented. It should probably mention it uses String#upto for String and Symbol.

P.S.3: Range uses #succ and #<=>, which might not be coherent as we see in the case of String. How to solve that?
----------------------------------------
Feature #5607: Inconsistent reaction in Range of String
http://redmine.ruby-lang.org/issues/5607

Author: Yen-Nan Lin
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


=begin
When I tried to access excel file, I found some inconsistent behavior about range of string. 

ruby-1.9.3-p0 :001 > ("A".."AB").to_a
 => ["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"]

This behavior is as what I thought.

ruby-1.9.3-p0 :002 > ("X".."AB").to_a
 => []

However, I tried to access "X" to "AB", and its reaction is inconsistent with above example.

I hope that behavior would be consistent in future release.

Thanks!
=end



-- 
http://redmine.ruby-lang.org