My preferred way is to not worry about escaping and instead use %q
, which behaves like a single-quote string (no interpolation or character escaping), or %Q
for double quoted string behavior:
str = %q[ruby 'on rails" ] # like single-quoting
str2 = %Q[quoting with #{str}] # like double-quoting: will insert variable
See https://docs.ruby-lang.org/en/trunk/syntax/literals_rdoc.html#label-Strings and search for % strings
.