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

**URL:** <https://www.ruby-forum.com/t/ok-so-no-kcode-a-chance-to-learn-a-little-about-encodings/194188>\
**Category:** Ruby\
**Created:** [August 31, 2010, 6:52pm UTC](https://www.ruby-forum.com/t/ok-so-no-kcode-a-chance-to-learn-a-little-about-encodings/194188 "2010-08-31T18:52:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 31, 2010, 6:52pm UTC](https://www.ruby-forum.com/t/ok-so-no-kcode-a-chance-to-learn-a-little-about-encodings/194188/1 "2010-08-31T18:52:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tcsnide](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@tcsnide](https://www.ruby-forum.com/u/tcsnide)\
**Post date:** [August 31, 2010, 8:18pm UTC](https://www.ruby-forum.com/t/ok-so-no-kcode-a-chance-to-learn-a-little-about-encodings/194188/2 "2010-08-31T20:18:14Z")

</div>

On Aug 31, 12:47 pm, Intransition [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 think I have figured it out. Given two strings to compare we can do:

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

s = str1.codepoints.to_a
t = 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](http://blog.grayproductions.net/articles/ruby_19s_string)
