Why is cutting an array out of range not returning nil?

Asked

Viewed 23 times

1

The ruby documentation says that when trying to "cut" an array using an invalid index it will return nil, so because I always get a number array in the index after the last valid index?

array = [0, 1, 2]

# retornando um array vazio como esperado
p array[2,0] # []

# o índice 3 não existe então por que ele retorna '[]'?
p array[3,0] # []

# fora do intervalo retorne 'nil'
p array[4,0] # nil

1 answer

0

The method you are calling is this array[start, length].

The documentation says:

If start == self.size and length >= 0, Returns a new Empty Array.

That is, if start is the size of the array and length is greater than or equal to zero, so it returns a new empty array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.