And your answer for Python is not quite correct: "" is falsy in Python, and both of the last two when translated to Python give "null".
And your answer for Python is not quite correct: "" is falsy in Python, and both of the last two when translated to Python give "null".
What gives you that impression? "abc".slice(4, 10) is perfectly valid and accepted, assuming the code above is accurate.
The inconsistency here is that when you call "abc".slice(2, 10) and get "c", Ruby has implicitly truncated the range to return whatever characters are available, even though it can't go all the way to 10 because the string isn't long enough. But then when you call "abc".slice(4, 10), it doesn't just give you all available characters from index 4 (which would be an empty string), it gives you null instead.
I don't see the inconsistency. slice on Array works the same way. Where is the inconsistency?
> (which would be an empty string)
What other aspect of Ruby would suggest that it is an empty string?
If what you are struggling to say is that different languages are different, then okay. "Japanese is unlike the English I know and therefore is inconsistent" would be a rather bizarre take, though.
Between what happens when the start index is greater than the length of the input, and what happens when the end index is greater than the length of the input. If the end index is greater than the length of the input, it returns a string (as long as the start index is not greater than the length of the input). But if the start index is greater than the length of the input, it does not return a string: it returns null, which is not a string.
My suggestion is that the behavior would have made more sense if it either returned a string in both cases (i.e., if it returned a string even if the start index is greater than the length of the input), or returned null in both cases (i.e., if it returned null whenever the end index is greater than the length of the input).
Again, what makes that an inconsistency and not just a different language?
> My suggestion is that the behavior would have made more sense
On the basis of the start and end indices being equivalent. But are they? What attributes of the language should see us consider them to be?
> What attributes of the language should see us consider them to be?
None.
I'm not sure where "care" enters into the picture. It's a computer language. For what reason would emotions be assigned to it?
That's why if you put five or more in for the first index it fails to produce a result entirely. I think I might I preferred an exception or a failure code being returned, but I can't say the current design is truly awful.
Where does this come from? Are these discrepancies stemming from different Ruby implementations/versions behaving differently? "abc".slice(5, 10) returns the same value as "abc".slice(4, 10) [which, curiously, does not return the same value as the original comment] under MRI 2.6.1 that I had handy.
I ask for a range whose start is in bounds but whose end is out of bounds.
Why should those return two entirely different types?
And yes, I also understand the logic of the API; but if you're used to using slice to protect against random NPEs and out-of-bounds exceptions - which is something I do and am used to being able to trust in as a general pattern.