Ruby Output Unicode Character

Jeremy Harris picture Jeremy Harris · Aug 28, 2013 · Viewed 48.7k times · Source

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.

The problem is if I do:

checkmark = "\u2713"
puts checkmark

It outputs "\u2713" instead of ✓

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

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

EDIT


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


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

Unicode Check Mark U+2713

Answer

falsetru picture falsetru · Aug 28, 2013

In Ruby 1.9.x+

Use String#encode:

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

prints

In Ruby 1.8.7

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