Created
December 31, 2013 19:48
-
-
Save coffeencoke/8201402 to your computer and use it in GitHub Desktop.
Why does this happen?
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
"'this has quotes'".gsub("'", "\\'") # => "this has quotes'this has quotes" |
Simple answer: because you have one too many backslashes.
Longer answer: Because ' in gsub means "the first part of the match."
@ggoodale how can I have it result in "'this has quotes'" ?
Right, missed that you wanted the backslashes. :) For that, you can use %q:
"'this has quotes'".gsub("'", %q(\\\'))
note that if you're testing this in irb, you'll see double backslashes; stick puts
in front to verify that you're getting the right output.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why does this not produce: "'this has quotes'"
?