Actually, you don't need \uxxxx
notation - this is for display purposes in Ruby. Try CGI.unescapeHTML(string_with_entities)
from built-in CGI module.
Need to convert string "Θ" to "\u0398"
-
17-06-2023 - |
Question
My Rails application stores strings containing html entity codes, e.g. "Θ", which display Greeks and other characters on browser pages. To display these same characters in Prawn documents, I need to convert "Θ" to "\u0398". Using a regexp I can extract the bare codepoint, "0398", from the original string. But I'm unable to use this to create a new string variable containing "\u0398".
I've tried many variations of string concatenation, interpolation and even array operations, but no joy. Anything that looks like
new_string_var = "\u" + my_codepoint
generates an "invalid Unicode escape" error at "\u".
Anything that looks like
new_string_var = "\\u" + my_codepoint
runs without error but inserts the literal string "\u0398" in the Prawn document.
Is it possible in Ruby to construct a string like this? Is there a better approach?
La solution