Skip to content

Instantly share code, notes, and snippets.

@joshgoebel
Created February 18, 2017 20:15
Show Gist options
  • Save joshgoebel/99d1bd79193e2b21c60f667a9c0a3cb9 to your computer and use it in GitHub Desktop.
Save joshgoebel/99d1bd79193e2b21c60f667a9c0a3cb9 to your computer and use it in GitHub Desktop.
def split(str, delimiters)
delimiters = delimiters.split(//)
result, acc = [], ""
str.split(//).each do |c|
if delimiters.include?(c)
# add the acculumator and the delimiter to our growing output array
unless acc.empty?
result << acc
acc = "" # reset accumulator
end
result << c
else
# append our character to our accumulator
acc << c
end
end
# finally add any left over string to the end
result << acc unless acc.empty?
result
end
puts split("josh-becky%colleen*nella&emmet","%-*&").inspect
# ["josh", "-", "becky", "%", "colleen", "*", "nella", "&", "emmet"]
puts split("josh-becky%colleen*nella&emmet","%").inspect
# ["josh-becky", "%", "colleen*nella&emmet"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment