Given
array = [:a,:b,:c,:d] # with 4 elements:
array[3] => :d # zero-based indices, I get that
array[4] => nil
array[5] => nil
array[3,0] => [] # OK since I asked for a slice with zero elements
Doco for array[start,length] says that it "Returns nil if the index (or starting index) are out of range."
array[5,0] => nil # OK
array[4,0] => [] # Hunh??
How come array[4,0]
returns an array as opposed to nil
?
[edit] Looks like this has already come up: see Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)
To me the explanation looks a bit like hand waving, but I'll settle for it and just accept that ruby violates PLS here.