To get the last object of an array in JS is:
arr = [1, 2, 3]
arr[arr.length - 1]
To get the last object of an array in Python is: arr = [1, 2, 3]
arr[len(arr) - 1]
To get the last object of an array in Lua is: arr = {1, 2, 3}
arr[#arr]> non-string objects
There is no difference between array and string access in ruby, python, js.
There are better ways to access last element (python, ruby):
>>> arr[-1] # meaning first element from back (1-indexing!)
3
I mentioned your comment is wrong. I do not know why you are turning it into 0-indexing vs 1-indexing, looks like changing topic for me.[1] https://en.wikipedia.org/wiki/Comparison_of_programming_lang...
... That's exactly where this topic started:
> 1-index kept throwing me off though
---
> There is no difference between array and string access in ruby, python, js.
There's no difference in Lua either:
s:sub(1, 1)
tbl:select(1, 1)
But it doesn't use offset, and I find that more useful for preventing off-by-one errors.>>>> a string will always be:
s:sub(1, #s)
>>> In a language that used the half-open interval convention we would end up with: s:sub(0, #s)
>> You would end up with: s:sub(0, #s - 1)
> That would be strange, in Ruby s[0, s.length]
Your argument against 0-index is just plain wrong. No one creates such API. It would be to awkward - either #s should be `length + 1` or `sub` should expect `length + 1`.----
>>>> Once you get used to it, ... you don't need math when grabbing a range.
I assure you once you get used to 0-indexing there is no need for math when grabbing a range
s[0..s.length] # that is identity through range in ruby
---->>>>> 1-index kept throwing me off though
>>>> it does help prevent a whole bunch of off by one errors
You claim superiority, it is not. I can't remember such errors in my life. It may be easier to describe to children but I doubt it. My first encounter with 0-indexing was with ruler - "why it starts with 0?" - pre-school. I can claim 0-index is natural. I would not.
There is no better model just "happy accidents".