Print Unicode escape codes from variable

微笑、不失礼 提交于 2019-12-31 04:39:12

问题


I have a list of Unicode character codes that I would like to output with rumoji. 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 = [
    "1F600",
    "1F476",
    "1F474"
]

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

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


回答1:


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:

▶ data.map { |e| e.to_i(16) }.pack 'U*'
#⇒ "😀👶👴"


来源:https://stackoverflow.com/questions/37020444/print-unicode-escape-codes-from-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!