Encoding::ASCII_8BIT is a special encoding that is usually
used for a byte string, not a character string. But as the name insists,
its characters in the range of ASCII are considered as ASCII characters.
This is useful when you use ASCII-8BIT characters with other ASCII
compatible characters.

The associated Encoding of a String can be changed in two different ways.

First, it is possible to set the Encoding of a
string to a new Encoding without changing the
internal byte representation of the string, with String#force_encoding. This
is how you can tell Ruby the correct encoding of a string.

Second, it is possible to transcode a string, i.e. translate its internal
byte representation to another encoding. Its associated encoding is also
set to the other encoding. See String#encode for the various forms
of transcoding, and the Encoding::Converter class for additional
control over the transcoding process.

All Ruby script code has an associated Encoding
which any String literal created in the source
code will be associated to.

The default script encoding is Encoding::UTF-8 after v2.0, but
it can be changed by a magic comment on the first line of the source code
file (or second line, if there is a shebang line on the first). The comment
must contain the word coding or encoding,
followed by a colon, space and the Encoding
name or alias:

# encoding: UTF-8"some string".encoding#=> #<Encoding:UTF-8>

The __ENCODING__ keyword returns the script encoding of the
file which the keyword is written:

# encoding: ISO-8859-1__ENCODING__#=> #<Encoding:ISO-8859-1>

ruby -K will change the default locale encoding, but this is
not recommended. Ruby source files should declare its script encoding by a
magic comment even when they only depend on US-ASCII strings or regular
expressions.

Each IO object has an external encoding which
indicates the encoding that Ruby will use to read its data. By default Ruby
sets the external encoding of an IO object to the
default external encoding. The default external encoding is set by locale
encoding or the interpreter -E option. ::default_external
returns the current value of the external encoding.

The default external encoding may also be set through ::default_external=,
but you should not do this as strings created before and after the change
will have inconsistent encodings. Instead use ruby -E to
invoke ruby with the correct external encoding.

When you know that the actual encoding of the data of an IO object is not the default external encoding, you can
reset its external encoding with IO#set_encoding or set it at IO object creation (see IO.new options).

To process the data of an IO object which has an
encoding different from its external encoding, you can set its internal
encoding. Ruby will use this internal encoding to transcode the data when
it is read from the IO object.

Conversely, when data is written to the IO object it
is transcoded from the internal encoding to the external encoding of the IO object.

The internal encoding is optional and when not set, the Ruby default
internal encoding is used. If not explicitly set this default internal
encoding is nil meaning that by default, no transcoding
occurs.

The default internal encoding can be set with the interpreter option
-E. ::default_internal
returns the current internal encoding.

The default internal encoding may also be set through ::default_internal=,
but you should not do this as strings created before and after the change
will have inconsistent encodings. Instead use ruby -E to
invoke ruby with the correct internal encoding.

While writing the file, the internal encoding is not specified as it is
only necessary for reading. While reading the file both the internal and
external encoding must be specified to obtain the correct result.

Sets default external encoding. You should not set ::default_external in
ruby code as strings created before changing the value may have a different
encoding from strings created after the value was changed., instead you
should use ruby -E to invoke ruby with the correct
default_external.

Sets default internal encoding or removes default internal encoding when
passed nil. You should not set ::default_internal in
ruby code as strings created before changing the value may have a different
encoding from strings created after the change. Instead you should use
ruby -E to invoke ruby with the correct default_internal.