Reputation: 4970
I found some nice Unicode characters on a web site that I would like to use in my Ruby program, but they only include the visual character and not the numeric code I would use in the "\u...." form of expressing it. I would prefer to use the "\u1234" form when I specify it in the source code so that any font can display it. How can I derive this "\u..." value from the original character (e.g. "⬆")?
Upvotes: 1
Views: 1124
Reputation: 4970
The numeric string to go after the "\u" can be computed in 2 steps:
1) call ord
to get the numeric value of the character
2) call to_s(16)
to convert that to a hex representation
[11] pry(main)> '⬆'.ord.to_s(16)
"2b06"
[12] pry(main)> "\u2b06"
"⬆"
Upvotes: 2