Jignesh Gohel
Jignesh Gohel

Reputation: 6552

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.

Upvotes: 5

Views: 3467

Answers (2)

Stefan
Stefan

Reputation: 114138

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 #=> "अ"

Upvotes: 5

Uri Agassi
Uri Agassi

Reputation: 37409

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")
=> "अ"

Upvotes: 1

Related Questions