escaping a string in Ruby

后端 未结 4 631
有刺的猬
有刺的猬 2021-02-05 16:39

I want to insert the following as the value for a variable in some Ruby:

`~!@#$%^&*()_-+={}|[]\\:\";\'<>?,./

Surrounding this in doub

4条回答
  •  轮回少年
    2021-02-05 17:21

    First, unless I'm crazy %q() does work here, perfectly well, since the inner parentheses are balanced:

    >> weird = %q(`~!@#$%^&*()_-+={}|[]\:";'<>?,./)
    => "`~!@\#$%^&*()_-+={}|[]\\:";'<>?,./"
    >> puts weird
    `~!@#$%^&*()_-+={}|[]\:";'<>?,./
    

    As a side note: when using %q, you always have the nuclear option of using spaces as the delimiter. This is foul and officially a Bad Idea, but it works.

    Again, I wouldn't do it, but just to see...

    >> weird = %q `~!@#$%^&*()_-+={}|[]\:";'<>?,./ 
    => "`~!@\#$%^&*()_-+={}|[]\\:";'<>?,./"
    

提交回复
热议问题