Ruby trying to dynamically create unicode string throws "invalid Unicode escape" error
dynamic, ruby, string, unicode
Solution
You can pass an encoding to `Integer#chr`:
hex = 0x0905
hex.chr('UTF-8') #=> "अ"
The parameter can be omitted, if `Encoding::default_internal` is set to UTF-8:
$ ruby -E UTF-8:UTF-8 -e "p 0x0905.chr"
"अ"
You can also append codepoints to other strings:
'' << hex #=> "अ"
Problem
I have a requirement wherein I want to dynamically create a unicode string using interpolation.For e.g. please see the following code tried out in irb ``` 2.1.2 :016 > hex = 0x0905 => 2309 2.1.2 :017 > b = "\u#{hex}" SyntaxError: (irb):17: invalid Unicode escape b = "\u#{hex}" ``` The hex-code 0x0905 corresponds to unicode for independent vowel for DEVANAGARI LETTER A. I am unable to figure how to achieve the desired result.