tags:

views:

27

answers:

1

An irb session is following:

>> ar = [:peanute, :butter, :and, :jelly]
=> [:peanute, :butter, :and, :jelly]
>> ar[0, 1]
=> [:peanute]
>> ar[4, 1]
=> []
>> ar[5, 1]
=> nil

Why ar[4, 1] does not return nil just like ar[5, 1]? Would someone explain this behaviour?

+2  A: 

This is mentioned in the documentation as a special case.

khelll
Special cases make programmers' lives miserable.
Waseem
Mentioned but not justified. "Returns nil if the index (or starting index) are out of range": in ar[4,1], this is so, yet it doesn't return nil, as it should. Very, very unusual for Ruby to suck in this manner. Maybe it's an "off-by-one" error ion the Ruby specs?
Brent.Longborough
I totally agree that this a non consistent behaviour which is misleading as well...
khelll