String from code points?


#1

Given a(n array of) numeric value(s) representing (a) Unicode code
point(s), how do I get a string of the indicated character(s)?

Integer#chr doesn’t seem to know about encodings; it just outputs the
byte value (and fails for values that don’t fit in a byte).

Array#pack(‘U*’) yields a UTF-8 string, but it’s marked as
ASCII-8BIT.

Is there a preferred method?


#2

Hi,

In message “Re: String from code points?”
on Sat, 2 May 2009 05:45:04 +0900, Marcos removed_email_address@domain.invalid
writes:

|Given a(n array of) numeric value(s) representing (a) Unicode code
|point(s), how do I get a string of the indicated character(s)?
|
|Integer#chr doesn’t seem to know about encodings; it just outputs the
|byte value (and fails for values that don’t fit in a byte).

p 12354.chr
=> 12354 out of char range (RangeError)

p 12354.chr(“utf-8”)
=> e$B$"e(B


#3

On May 2, 8:25 am, Yukihiro M. removed_email_address@domain.invalid wrote:

p 12354.chr
=> 12354 out of char range (RangeError)

p 12354.chr(“utf-8”)
=> e$B$"e(B

If the error happens then does it mean the ruby(http://
tryruby.hobix.com/) is using a old version? what version I should use
instead?

p 12354.chr(“utf-8”)
ArgumentError: wrong number of arguments (1 for
0)
from (irb):3:in
`chr’
from (irb):
3
from :0


#4

Ken wrote:

If the error happens then does it mean the ruby(http://
tryruby.hobix.com/) is using a old version?

Type RUBY_DESCRIPTION to see that it’s running 1.8.7

All this encoding stuff requires 1.9.x. Matz’s example works on 1.9.1
and 1.9.2.