Ruby HTML 实体问题
Ruby 新手,我想知道如何获取以下内容来仅打印度数符号...
require 'htmlentities'
coder = HTMLEntities.new
puts coder.decode('°')
当前命令行 (Windows) 输出是: °
谢谢!
New to Ruby and am wondering how to get the following to print just the degree symbol...
require 'htmlentities'
coder = HTMLEntities.new
puts coder.decode('°')
Currently the command line (Windows) output is: °
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来
HTMLEntities.decode
返回一个 UTF-8 格式的字符串,而您的控制台因该编码而令人窒息。在将字符串传递给puts
之前,您必须对其进行重新编码。如果您使用的是 Ruby 1.9.2,代码看起来相当简单(基于 字符串和编码文档):
在找到控制台可以理解的内容之前,您可能需要尝试几种不同的编码。
如果您使用旧版本的 Ruby,看起来可以通过 Iconv (参见 这个问题 - 我怀疑你只是朝相反的方向走)。
希望这有帮助!
It looks like
HTMLEntities.decode
returns a string in UTF-8, and your console is choking on that encoding. You'll have to re-encode your string before passing it toputs
.If you're using Ruby 1.9.2, it looks like the code is fairly straightforward (based on the String and Encoding documentation):
You might have to try a couple different encodings before you find something your console can understand.
If you're on an older version of Ruby, it looks like the re-encoding is doable through Iconv (see this question - I suspect you're just going in the opposite direction).
Hope this helps!