Refactor of my last PR to remove warnings for ruby >= 1.9

My last PR was throwing a bunch of warnings in ruby >= 1.9 when running the tests since it was accessing `$KCODE`.

This refactors that code so that `$KCODE` is not touched whatsoever unless ruby is < 1.9. This removes the warnings.

@jduff