String#split behavior
Standard case
'x:y'.split(':') # => ['x', 'y']
Corner cases
''.split(':') # => []
':'.split(':') # => []
'x'.split(':') # => ['x']
'x:'.split(':') # => ['x']
':x'.split(':') # => ['', 'x']
'::'.split(':') # => []
'x::y'.split(':') # => ['x', '', 'y']
'::x'.split(':') # => ['', '', 'x']
'x::'.split(':') # => ['x']
Takeaways
For the following discussion, a “segment” is defined as the (possibly empty) string between separators or at the start or end of the string.
- The result is always an array.
- The only time an empty string will appear in the resulting array is when one or more empty segments are followed by a
non-empty segment. Some notable implications:
- A trailing separator will not produce an empty string in the result.
- If all the segments in the string are empty, the resulting array will always be empty.