idleberg idleberg - 1 year ago 110
Ruby Question

Print Unicode escape codes from variable

I have a list of Unicode character codes that I would like to output with

. Here's the code I'm using to iterate over my data.

require "rumoji"

# this works
puts Rumoji.decode("\u{1F600}")

# feed some data
data = [

data.each do |line|
# this doesn't work
puts Rumoji.decode("\u{#{line}}")
puts Rumoji.decode("\u{" + line + "}")

I'm not sure how I can use variable names inside the escaped string.

Answer Source

One can not use \u along with string interpolation, since \u takes precedence. What one might do, is to Array#pack an array of integers:

▶ { |e| e.to_i(16) }.pack 'U*'
#⇒ "
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download