this question has answer here:
for four-element array, [4] returns nil, [4, 0] returns empty array.
array = [:peanut, :butter, :and, :jelly] array[4] # => nil array[4, 0] # => [] array[5] # => nil array[5, 0] # => nil why [4, 0] not return nil [5, 0]?
answer array slicing in ruby: looking explanation illogical behaviour (taken rubykoans.com) => graphical explination of whats happening
it's special case. official docs (the italics mine):
for start , range cases starting index before element. additionally, empty array returned when starting index element range @ end of array.
a = [ "a", "b", "c", "d", "e" ] # ... # special cases a[5] #=> nil a[6, 1] #=> nil a[5, 1] #=> []
Comments
Post a Comment