Just because you've done something similar before, doesn't mean you
should trust yourself to code it from memory and get it right without
testing....
class String
def split_escapable(split_char, escape_char)
arr = []
split(split_char).each do |x|
if(arr[-1] && (arr[-1][-1].chr == escape_char))
arr[-1].chop!
arr[-1] << x
else
arr << x
end
end
arr
end
end
It's not very good, because you can't escape the backslash, but some
magic should sort that out, and depending on what you're doing, you
might not care. Also, it's not all that elegant, so maybe you already
got this kind of solution.
Also, you should note that here:
"Hello\, World,Hi".split_escapable(',' '\')
# => ["Hello, World", "Hi"]
Since you're using double-quotes, the backslash is already being
consumed as an escape character, and it won't compile because the
backslash in the single quotes needs to be escaped because it preceeds a
single quote. And also because you missed the comma between arguments.
'Hello\, World,Hi'.split_escapable(',','\\')
# => ["Hello, World", "Hi"]
works.
···
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################