On Tue, 22 Feb 2005 00:31:42 +0900, Navindra Umanee
<navindra / cs.mcgill.ca> wrote:
> Austin Ziegler <halostatue / gmail.com > wrote:
>> On Mon, 21 Feb 2005 06:07:58 +0900, Navindra Umanee
>> <navindra / cs.mcgill.ca > wrote:
>>> Often think it would be nice if "" and 0 were treated like nil.
>>> Such functions could then return "". Heck, NilClass.to_s and
>>> NilClass.to_i already return "" and 0 respectively.
>> I'm far happier that they aren't.
> Maybe what is more annoying, is this type of inconsistency:
> 
> irb(main):003:0> list[4..5]
> => nil
> irb(main):004:0> list[0..3]
> => []
> 
> Ruby is riddled with these and IMHO it tends to make code that much
> more ugly.

That's not inconsistent; you're just not understanding. Matz posted
about this some time back and its a perfectly clear and consistent
way of working. Let's say you have an empty Array, 'a':

    a = []
    a[0]    # nil
    a[1]    # nil

That's as expected. So why does this do something that you don't
expect?

    a[0..3] # []
    a[1..3] # nil

Well, it's easier to look at with a non-empty array. Let's try:

    a = %w(a b c)
    a[0]            # a
    a[1]            # b
    a[2]            # c
    a[3]            # nil

Okay, that again is as expected. But where do the indexes point?
According to matz, the indexes are conceptually *between* elements.
Something like:

        a   b   c
      0^  1^  2^  3^..

So when you start your range outside of the allowable indexes (and
the size of the array is an allowable, but empty, index), then
you'll get +nil+ from the #[] call. So for our above array, a[0..3]
produces the expected result, a[3..9] produces an empty array, and
a[4..9] produces nil.

It's regular. It's explained. It makes sense. It just doesn't work
like a P language. I doubt you'll find many experienced Ruby
programmers -- or novice Ruby programmers who don't expect Ruby to
act like a P language -- who are confused about this issue.

-austin
-- 
Austin Ziegler * halostatue / gmail.com
               * Alternate: austin / halostatue.ca