4 ms·
That would be strange, in Ruby s = "foo" s.length #=> 3 s[0, s.length] #=> "foo" Same in JavaScript s.slice(0, s.length) and Python
by sergeykish 6y ago
That would be strange, in Ruby
s = "foo"
s.length
#=> 3
s[0, s.length]
#=> "foo"
Same in JavaScript
s.slice(0, s.length)
and Python
s[0:len(s)]
- shakna 6y agoYet, none of those are consistent with that on non-string objects: 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]
- sergeykish 6y agoThere is a difference between order and offset, both consistent and makes sense. We use both models in everyday life. Examples of offset are ruler (marks 0, 1, ...), time offset (this hour, next hour, ..), age (newborn, one year, two years), count (last, last but one). That's reflected in our languages [1]. > 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_languages_(array) https://en.wikipedia.org/wiki/Comparison_of_programming_lang...
- shakna 6y ago> 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. ... 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.
- sergeykish 6y agoI am to used to 0-indexing to judge. If it is not clear already - I am not going to participate in who is better. My thread is >>>> 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".