Best way to escape and unescape strings in Ruby?

Ruby 2.5 added String#undump as a complement to String#dump: $ irb irb(main):001:0> dumped_newline = “\n”.dump => “\”\\n\”” irb(main):002:0> undumped_newline = dumped_newline.undump => “\n” With it: def escape(s) s.dump[1..-2] end def unescape(s) “\”#{s}\””.undump end $irb irb(main):001:0> escape(“\n \” \\”) => “\\n \\\” \\\\” irb(main):002:0> unescape(“\\n \\\” \\\\”) => “\n \” \\”

How can I escape a single quote in a single-quote string in Bash?

echo ‘I\’m a student’ does not work. But the following works: echo $’I\’m a student’ From the man page of bash: A single quote may not occur between single quotes, even when preceded by a backslash. …. Words of the form $’string’ are treated specially. The word expands to string, with backslash-escaped characters replaced as … Read more