The ruby docs for the String class’s split method state:
[If limit is] negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.
So as far as I can tell there is no difference between any of the following:
string="1,,2,3,,,4,,5,,6"
string.split(",", -1)
string.split(",", -4)
string.split(",", -1000000)
They all return the same value: ["1", "", "2", "3", "", "", "4", "", "5", "", "6"]
Just wondering why it would be possible to assign different negative limits in this way.
Also, what happens if I do want to remove the trailing null values (like whitespace)? I know I can do string.strip.split(",",-1) but is there a way to do it without using strip method?
By both your testing and your quoted documentation, there appears to be no difference between
string.split(",", -1)andstring.split(",", -4). The specification says there is no difference and you found no difference in testing. Hooray.But note that
string.strip.split(..)won’t remove the trailing null values:You’ll have to pick another mechanism to strip the nulls.