Ok, so no $KCODE --a chance to learn a little about Encodings

I

Intransition

I am working on some 1.8 to 1.9 compatibility. I have a bit of code:

if $KCODE =~ /^U/i
unpack_rule = 'U*'
else
unpack_rule = 'C*'
end

$KCODE is no long effective, so what to do for 1.9?
 
I

Intransition

I am working on some 1.8 =A0to 1.9 compatibility. I have a bit of code:

=A0 =A0 if $KCODE =3D~ /^U/i
=A0 =A0 =A0 unpack_rule =3D 'U*'
=A0 =A0 else
=A0 =A0 =A0 unpack_rule =3D 'C*'
=A0 =A0 end

$KCODE is no long effective, so what to do for 1.9?

I think I have figured it out. Given two strings to compare we can do:

if str2.encoding !=3D str1.encoding
str2 =3D str2.encode(str1.encoding.name)
end

s =3D str1.codepoints.to_a
t =3D str2.codepoints.to_a

Where #codepoints does the same as old str.unpack(unpack_rule).

This page was very helpful:

http://blog.grayproductions.net/articles/ruby_19s_string
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,828
Members
47,374
Latest member
anuragag27

Latest Threads

Top