Ruby Output Unicode Character
Asked Answered
V

6

50

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

Variegation answered 28/8, 2013 at 15:50 Comment(0)
S
59

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') }
✓
Smithson answered 28/8, 2013 at 15:53 Comment(8)
I should have mentioned I have Ruby 1.8.7 and apparently the encode method isn't available until 1.9. How was it done prior to 1.9?Variegation
@cillosis, I added Ruby 1.8.7 compatible version. You should use '\u2713' or "\\u2713", because "\u2713" == "u2713" in ruby 1.8.Smithson
Bam! I first tried your solution and it wasn't working. Then went to single quotes and it worked! Thanks :)Variegation
I was able to simply do something like checkmark = "\u2713".encode('utf-8'); puts checkmark and so the encoding was saved to the variable. This works for me in Ruby 2.2.2.Smart
@sixty4bit, If you want to notify OP, mention OP or comment on the question. Otherwise, OP will not be notified.Smithson
checkmark = "\u2663" puts checkmark.encode('utf-8') prints ♣ for me!Imperium
"\u127968".encode("utf-8") is giving me ቹ68. I was hoping for 🏠. Is there a Ruby way to print the upper ranges of Unicode?Mispronounce
@Lori, "\u{1f3e0}" ('1f3e0' == 127968.to_s(16))Smithson
W
24

In newer versions of Ruby, you don't need to enforce encoding. Here is an example with 2.1.2:

2.1.2 :002 > "\u00BD"
 => "½"

Just make sure you use double quotes!

Woodhouse answered 22/4, 2016 at 16:39 Comment(1)
This comment about making sure to use double-quotes saved me -- I was tearing my hair out trying to figure out why this wasn't working!Christianism
C
18

falsetru's answer is incorrect.

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

This transcodes the checkmark from the current system encoding to UTF-8 encoding. (That works only on a system whose default is already UTF-8.)

The correct answer is:

puts checkmark.force_encoding('utf-8')

This modifies the string's encoding, without modifying any character sequence.

Cordiacordial answered 10/12, 2014 at 15:23 Comment(0)
H
5

As an additional note, if you want to print an emoji, you have to surround it with braces.

irb(main):001:0> "\u{1F600}"
=> "😀"
Hypergolic answered 4/9, 2019 at 4:37 Comment(2)
Nice but doesn't seem to work with string interpolation a=1F600; puts "\u{#{a}}"Romie
@Romie I found a way to do it: a = '1F600'; puts a.to_i(16).chr('UTF-8'). Outputs 😀.Delphine
V
0

Same goes as above in ERB, no forced encoding required, works perfectly, tested at Ruby 2.3.0

    <%= "\u00BD" %>

Much appreciation

Viosterol answered 7/7, 2016 at 17:56 Comment(0)
M
0

How we can print as is in newer version of ruby

checkmark = "\u2713" puts checkmark

This should print AS IS \u2713

Mathis answered 17/12, 2021 at 19:27 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Moleskin

© 2022 - 2024 — McMap. All rights reserved.