2 ms·
While there's no requirement that a Ruby Array is implemented as an array in the implementation language (C, C++, Java, etc.), in practice it is in all the Ruby
by nirvdrum 9y ago
While there's no requirement that a Ruby Array is implemented as an array in the implementation language (C, C++, Java, etc.), in practice it is in all the Ruby implementations. In order to traverse an array, you need to know the length so you can ensure your array index is in bounds. I.e., to read the first element of an array, you'll need to read the size anyway. That being the case, you would want to compare the size to 0 because comparing ints is much faster than reading from memory.