I want to insert the following as the value for a variable in some Ruby:
`~!@#$%^&*()_-+={}|[]\:";'<>?,./
Surrounding this in double quotes doesn’t work, so is there a nice escape_until_the_end sort of thing available?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Don’t use multiple methods – keep it simple.
Escape the #, the backslash, and the double-quote.
Or if you don’t want to escape the # (the substitution character for variables in double-quoted strings), use and escape single quotes instead:
%q is great for lots of other strings that don’t contain every ascii punctuation character. 🙂
Edit: Evidently you can used balanced parens inside %q() successfully as well, but I would think that’s slightly dangerous from a maintenance standpoint, as there’s no semantics there to imply that you’re always going to necessarily balance your parens in a string.