Problem
"aaa".split('a') == []
"aaa".split('a').join('a') == ""
Standard split is often ‘clever’, but not logical and not symmetric to join. To fix this here is a naive alternative that behaves ‘dumb’ but logical.
Solution
class String
# https://grosser.it/2011/08/28/ruby-string-naive-split-because-split-is-to-clever/
# " ".split(' ') == []
# " ".naive_split(' ') == ['','','','']
# "".split(' ') == []
# "".naive_split(' ') == ['']
def naive_split(pattern)
pattern = /#{Regexp.escape(pattern)}/ unless pattern.is_a?(Regexp)
result = split(pattern, -1)
result.empty? ? [''] : result
end
end