On this page
Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
Encoding
An Encoding instance represents a character encoding usable in Ruby. It is defined as a constant under the Encoding namespace. It has a name and, optionally, aliases:
Encoding::US_ASCII.name # => "US-ASCII" Encoding::US_ASCII.names # => ["US-ASCII", "ASCII", "ANSI_X3.4-1968", "646"]
A Ruby method that accepts an encoding as an argument will accept:
-
An Encoding object.
-
The name of an encoding.
-
An alias for an encoding name.
These are equivalent:
'foo'.encode(Encoding::US_ASCII) # Encoding object. 'foo'.encode('US-ASCII') # Encoding name. 'foo'.encode('ASCII') # Encoding alias.
For a full discussion of encodings and their uses, see the Encodings document.
Encoding::ASCII_8BIT is a special-purpose encoding that is usually used for a string of bytes, not a string of characters. But as the name indicates, its characters in the ASCII range are considered as ASCII characters. This is useful when you use other ASCII-compatible encodings.
Class methods 10
Instance methods 6
Ruby version changes 6
-
3.3
-
Encoding#replicatehas been removed, it was already deprecated. [Feature #18949]
-
-
3.2
-
Encoding#replicatehas been deprecated and will be removed in 3.3. [Feature #18949] -
The dummy
Encoding::UTF_16andEncoding::UTF_32encodings no longer try to dynamically guess the endian based on a byte order mark. UseEncoding::UTF_16BE/UTF_16LEandEncoding::UTF_32BE/UTF_32LEinstead. This change speeds up getting the encoding of aString. [Feature #18949] -
Limit maximum encoding set size by 256. If exceeding maximum size,
EncodingErrorwill be raised. [Feature #18949]
-
-
3.0
-
Added new encoding IBM720. [Feature #16233]
-
Changed default for
Encoding.default_externalto UTF-8 on Windows [Feature #16604]
-