3 ms·
Ruby happily allows this and I can't recall it ever being an issue. It's no more prone to errors than `x` being greater than the number of elements. > arr = ["
by caseyohara 4y ago
Ruby happily allows this and I can't recall it ever being an issue. It's no more prone to errors than `x` being greater than the number of elements.
> arr = ["a", "b", "c", "d", "e"]
> x = -2
> arr[x]
=> "d"
- edflsafoiewq 4y agoIf you intended to calculate an index and you accidentally get len(arr), you get a runtime error. But if you accidentally get -1 you silently get the last element instead. Similar to the argument about signed/unsigned indices in low level languages.
- ilyt 4y agoIf you calculated wrong you can land on positive-but-existing value just fine... > Similar to the argument about signed/unsigned indices in low level languages. Think that one has to do more with convenience where most of stuff uses int by default