I was very annoyed today to discover that the split method in Ruby, which turns strings into arrays, was behaving erratically when it came to blank fields at the end of the string. For instance:
"1,2,3" => [1,2,3] that is correct, but
"1,2,3," => [1,2,3] which is not, I'd expect a fourth, empty field.
Turns out that split has a "limit" parameter which controls this. Here's the doc for it:
If the limit parameter is omitted, trailing null fields are suppressed. If limit is a positive number, at most that number of fields will be returned (if limit is 1, the entire string is returned as the only entry in an array). If negative, there is no limit to the number of fields returned, and trailing null fields are not suppressed.
In other words if I do this, "1,2,3,".split(",",-1) then I get what I wanted - [1,2,3,'']
Not sure why that rule is there, but there ya go.