Monday, November 20, 2006

Ruby : Split ignores empty fields

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.


Technorati tags: , ,


zdennis said...

How limit works is annoying. It is also annoying that empty strings are kept at the beginning of a string IMO. ",1,2,3".split(/,/) results in [ "", "1", "2", "3"] whereas I wouldn't expect a leading empty string.

Anonymous said...

thanks, that saved my day!

lindes said...

This is probably to help mimic what ruby itself does when, for example, you do:

foo = [

In that case, an empty element at the end is not particularly expected. Same with "\n" as your split string, for most cases. If you have a file with 3 lines, and split its contents on "\n", you likely want 3 items.

Having the negative-number option is, of course, critical, though, because there are absolutely times when you *do* want 4 items from these examples (and/or trailing null items, in general). :)