如何输出转义字符?

时间:2016-12-16 19:07:57

标签: ruby linux string console escaping

我如何从

传递
a = "blog.posts.[activity]-It's-raining"

blog.posts.\[activity\]-It\'s-raining.md

那是带有转义符号的a吗?通常,必须在控制台(linux)中读取字符串a

1 个答案:

答案 0 :(得分:2)

您可以使用Shellwords.escape

require "shellwords"
a = "blog.posts.[activity]-It's-raining"
puts Shellwords.escape(a)

这导致:

$ ruby foo.rb
blog.posts.\[activity\]-It\'s-raining