Ruby Tuesday – String#split

Today’s Ruby Tuesday is on String#split.

String#split takes a pattern as it’s parameter, and uses that pattern as the deliminator[1] to split the string into an Array of tokens.

"It was the best of times, it was the worst of times".split
# => ["It", "was", "the", "best", "of", "times,", "it", "was",
#     "the", "worst", "of", "times"]
"It was the best of times, it was the worst of times".split(",")
# => ["It was the best of times", " it was the worst of times"]

If String#split is called with a single space as the pattern, it removes any leading and trailing whitespace, but if another pattern is specified, it will return empty strings representing a sequence of the deliminators.

"continuous        whitespace".split
# => ["continuous", "whitespace"]
" t  continuous t     whitespace t".split(" ")
# => ["continuous", "whitespace"]
# => ["fi", "", "le"]
"continuous        whitespace".split(/s/)
# => ["continuous", "", "", "", "", "", "", "", "whitespace"]

String#split can take a regular expression as the pattern in addition to taking a string.

# => ["fi", "", "le"]
=> ["fi", "", "le"]

If an empty pattern is passed to String#split then the string is split into a list of it’s characters.

# => ["f", "i", "z", "z", "l", "e"]
=> ["b", "a", "n", "a", "n", "a"]

String#split can take a second argument, which is a limit to the number of items returned in the array. A value of zero, behaves as if the limit was not passed as an argument. If the value for limit is positive it will return at most that many elements total, and will include any trailing empty strings, or “null fields” as called by the Ruby documentation, up to the limit. If a negative integer is passed as a value, all of the trailing empty strings that tokenized will be returned.

# => ["b", "n", "n"]
"banana".split("a", 0)
# => ["b", "n", "n"]
"banana".split("a", 1)
# => ["banana"]
"banana".split("a", 2)
# => ["b", "nana"]
"banana".split("a", 4)
# => ["b", "n", "n", ""]
"banana".split("a", 7)
# => ["b", "n", "n", ""]
"bananaaa".split("a", 7)
# => ["b", "n", "n", "", "", ""]
"banana".split("a", -1)
# => ["b", "n", "n", ""]
"banana".split("a", -2)
# => ["b", "n", "n", ""]
"banana".split("a", -4)
# => ["b", "n", "n", ""]
"bananaaaaaaa".split("a", -1)
# => ["b", "n", "n", "", "", "", "", "", "", ""]


[1] Deliminator – A portmanteau of delimiter and eliminate, signifying that the delimiter is to be removed from the results. back