From: Eric Hodel Date: 2008-09-11T10:44:12+09:00 Subject: [ruby-core:18554] Changing Encoding.default_external at runtime I'd like to be able to change Encoding.default_encoding for testing purposes. In particular, I'd like to write a unit test that ensures RubyGems loads .gemspec files without the magic comment as UTF-8. Something like: def test_self_load_specification_utf_8 orig_encoding = Encoding.default_external Encoding.default_external = Encoding.find 'ASCII' # ... test setup pi = "\317\200" pi.force_encoding 'UTF-8' assert_equal pi, Gem::SourceIndex.load_specification(spec_file).author ensure Encoding.default_external = orig_encoding end Currently the only way to do this is at ruby startup time through the LANG env var or -E. Is it too dangerous to add Encoding::default_external= ?