Created
February 18, 2017 20:15
-
-
Save joshgoebel/99d1bd79193e2b21c60f667a9c0a3cb9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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