I’m trying to do something like the following. Let’s say i have the following string:
"some string"
And i wanted to replace the space with a \1. However, wether i use single or double quote, i dont get the result:
"some string".gsub(" ", "\1") => "somestring"
"some string".gsub(" ", '\1') => "somestring"
"some string".gsub(" ", '\\1') => "somestring"
What i want is:
"some\1string"
Any suggestions?
This is only annoying because
\1through\9are reserved for use in substitutions.A possible solution is:
It’s kind of ugly, but it works.
An alternative is to use the block style where substitution isn’t performed:
Remember that the output will be
"some\\1string"because backslash is represented as\\inside a double-quoted string. If you print it out you will get a single backslash.