在 Ruby 中转义和取消转义字符串的最佳方法?
Ruby 是否有任何内置方法用于转义字符串和取消转义字符串?过去,我使用过正则表达式;然而,我发现 Ruby 可能一直在内部进行此类转换。也许这个功能暴露在某个地方。
到目前为止我已经想出了这些功能。它们有效,但看起来有点老套:
def escape(s)
s.inspect[1..-2]
end
def unescape(s)
eval %Q{"#{s}"}
end
有更好的方法吗?
Does Ruby have any built-in method for escaping and unescaping strings? In the past, I've used regular expressions; however, it occurs to me that Ruby probably does such conversions internally all the time. Perhaps this functionality is exposed somewhere.
So far I've come up with these functions. They work, but they seem a bit hacky:
def escape(s)
s.inspect[1..-2]
end
def unescape(s)
eval %Q{"#{s}"}
end
Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Ruby 2.5 添加了
String#undump
作为String#dump
:有了它:
Ruby 2.5 added
String#undump
as a complement toString#dump
:With it:
有很多转义方法,其中一些:
因此,这实际上取决于您需要解决的问题。但我会避免使用检查来转义。
更新 - 有一个转储,检查使用它,看起来这就是您所需要的:
There are a bunch of escaping methods, some of them:
So, its really depends on the issue you need to solve. But I would avoid using inspect for escaping.
Update - there is a dump, inspect uses that, and it looks like it is what you need:
Caleb 函数是我能找到的与 String #inspect 相反的最接近的函数,但它包含两个错误:
我修复了上述错误,这是更新版本:
Caleb function was the nearest thing to the reverse of String #inspect I was able to find, however it contained two bugs:
I fixed the above bugs and this is the updated version:
更新:我不再同意我自己的答案,但我不想删除它,因为我怀疑其他人可能会走上这条错误的道路,而且这个答案已经有很多讨论了它是替代方案,所以我认为它仍然有助于对话,但请不要在实际代码中使用这个答案。
如果您不想使用
eval
,但愿意使用YAML
模块,则可以使用它:YAML
的优势与 eval 相比,它可能更安全。cane
不允许使用eval
。我看到了使用$SAFE
和eval
的建议,但目前无法通过 JRuby 实现。就其价值而言,Python 确实对 转义反斜杠。
Update: I no longer agree with my own answer, but I'd prefer not to delete it since I suspect that others may go down this wrong path, and there's already been a lot of discussion of this answer and it's alternatives, so I think it still contributes to the conversation, but please don't use this answer in real code.
If you don't want to use
eval
, but are willing to use theYAML
module, you can use it instead:The advantage to
YAML
overeval
is that it is presumably safer.cane
disallows all usage ofeval
. I've seen recommendations to use$SAFE
along witheval
, but that is not available via JRuby currently.For what it is worth, Python does have native support for unescaping backslashes.
Ruby 的
inspect
可以提供帮助:通常,如果我们打印带有嵌入换行符的字符串,我们会得到:
如果我们打印检查的版本:
将检查的版本分配给变量,您将得到字符串的转义版本。
要撤消转义,请
eval
字符串:我不太喜欢这样做。这更多的是一种好奇心,而不是我在实践中会做的事情。
Ruby's
inspect
can help:Normally if we print a string with an embedded line-feed, we'd get:
If we print the inspected version:
Assign the inspected version to a variable and you'll have the escaped version of the string.
To undo the escaping,
eval
the string:I don't really like doing it this way. It's more of a curiosity than something I'd do in practice.
YAML 的
::unescape
似乎没有转义引号字符,例如'
和"
。我猜这是设计使然,但它让我难过。你绝对不想对任意或客户端提供的数据使用
eval
,这就是我所使用的。
YAML's
::unescape
doesn't seem to escape quote characters, e.g.'
and"
. I'm guessing this is by design, but it makes me sad.You definitely do not want to use
eval
on arbitrary or client-supplied data.This is what I use. Handles everything I've seen and doesn't introduce any dependencies.
我怀疑 Shellwords.escape 会做你正在寻找的事情
https://ruby-doc.org/stdlib-1.9.3/libdoc/shellwords/rdoc/Shellwords.html#method-c-shellescape
I suspect that
Shellwords.escape
will do what you're looking forhttps://ruby-doc.org/stdlib-1.9.3/libdoc/shellwords/rdoc/Shellwords.html#method-c-shellescape