Ruby输出Unicode字符 [英] Ruby Output Unicode Character

查看:271
本文介绍了Ruby输出Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按行业,我不是Ruby开发人员,但是我将Capistrano用于PHP部署.我正在尝试清除脚本的输出,并尝试添加 unicode复选标记,如此博客中所述.

I'm not a Ruby dev by trade, but am using Capistrano for PHP deployments. I'm trying to cleanup the output of my script and am trying to add a unicode check mark as discussed in this blog.

问题是如果我这样做:

checkmark = "\u2713"
puts checkmark

它输出"\ u2713"而不是✓

It outputs "\u2713" instead of ✓

我在Google周围四处搜寻,但找不到任何讨论此事的地方.

I've googled around and I just can't find anywhere that discusses this.

TLDR:如何putsprint Unicode复选标记U-2713?

TLDR: How do I puts or print the unicode checkmark U-2713?

编辑

我在Mac(OSX Lion)上运行Ruby 1.8.7,所以不能使用encode方法.我的外壳是iTerm2中的Bash.

I am running Ruby 1.8.7 on my Mac (OSX Lion) so cannot use the encode method. My shell is Bash in iTerm2.

更新 [4/8/2019]添加了参考图片,以防站点出现故障.

UPDATE [4/8/2019] Added reference image in case site ever goes down.

推荐答案

在Ruby 1.9.x +

使用 String#encode :

checkmark = "\u2713"
puts checkmark.encode('utf-8')

打印

在Ruby 1.8.7中

puts '\u2713'.gsub(/\\u[\da-f]{4}/i) { |m| [m[-4..-1].to_i(16)].pack('U') }
✓

这篇关于Ruby输出Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆