Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby trying to dynamically create unicode string throws "invalid Unicode escape" error

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.

like image 730
Jignesh Gohel Avatar asked Nov 25 '14 10:11

Jignesh Gohel


2 Answers

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 #=> "अ"
like image 118
Stefan Avatar answered Oct 14 '22 03:10

Stefan


String interpolation happens after ruby decodes the escapes, so what you are trying to do is interpreted by ruby like an incomplete escape.

To create a unicode character from a number, you need to pack it:

hex = 0x0905
[hex].pack("U")
=> "अ"
like image 39
Uri Agassi Avatar answered Oct 14 '22 02:10

Uri Agassi