Question

My Rails application stores strings containing html entity codes, e.g. "&#x0398", which display Greeks and other characters on browser pages. To display these same characters in Prawn documents, I need to convert "&#x0398" 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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top