Does anyone know of a Ruby gem (or built-in, or native syntax, for that matter) that operates on the outer quote marks of strings?
I find myself writing methods like this over and over again:
remove_outer_quotes_if_quoted( myString, chars ) -> aString
add_outer_quotes_unless_quoted( myString, char ) -> aString
The first tests myString
to see if its beginning and ending characters match any one character in chars
. If so, it returns the string with quotes removed. Otherwise it returns it unchanged. chars
defaults to a list of quote mark characters.
The second tests myString
to see if it already begins and ends with char
. If so, it returns the string unchanged. If not, it returns the string with char
tacked on before and after, and any embedded occurrance of char
is escaped with backslash. char
defaults to the first in a default list of characters.
(My hand-cobbled methods don't have such verbose names, of course.)
I've looked around for similar methods in the public repos but can't find anything like this. Am I the only one that needs to do this alot? If not, how does everyone else do this?