class 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
Encodingobject. -
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.
Constants
- ANSI_X3_4_1968
- ASCII
- ASCII_8BIT
- BIG5
- BIG5_HKSCS
- BIG5_HKSCS_2008
- BIG5_UAO
- BINARY
- Big5
- Big5_HKSCS
- Big5_HKSCS_2008
- Big5_UAO
- CESU_8
- CP1250
- CP1251
- CP1252
- CP1253
- CP1254
- CP1255
- CP1256
- CP1257
- CP1258
- CP437
- CP50220
- CP50221
- CP51932
- CP65000
- CP65001
- CP720
- CP737
- CP775
- CP850
- CP852
- CP855
- CP857
- CP860
- CP861
- CP862
- CP863
- CP864
- CP865
- CP866
- CP869
- CP874
- CP878
- CP932
- CP936
- CP949
- CP950
- CP951
- CSWINDOWS31J
- CsWindows31J
- EBCDIC_CP_US
- EMACS_MULE
- EUCCN
- EUCJP
- EUCJP_MS
- EUCKR
- EUCTW
- EUC_CN
- EUC_JISX0213
- EUC_JIS_2004
- EUC_JP
- EUC_JP_MS
- EUC_KR
- EUC_TW
- Emacs_Mule
- EucCN
- EucJP
- EucJP_ms
- EucKR
- EucTW
- GB12345
- GB18030
- GB1988
- GB2312
- GBK
- IBM037
- IBM437
- IBM720
- IBM737
- IBM775
- IBM850
- IBM852
- IBM855
- IBM857
- IBM860
- IBM861
- IBM862
- IBM863
- IBM864
- IBM865
- IBM866
- IBM869
- ISO2022_JP
- ISO2022_JP2
- ISO8859_1
- ISO8859_10
- ISO8859_11
- ISO8859_13
- ISO8859_14
- ISO8859_15
- ISO8859_16
- ISO8859_2
- ISO8859_3
- ISO8859_4
- ISO8859_5
- ISO8859_6
- ISO8859_7
- ISO8859_8
- ISO8859_9
- ISO_2022_JP
- ISO_2022_JP_2
- ISO_2022_JP_KDDI
- ISO_8859_1
- ISO_8859_10
- ISO_8859_11
- ISO_8859_13
- ISO_8859_14
- ISO_8859_15
- ISO_8859_16
- ISO_8859_2
- ISO_8859_3
- ISO_8859_4
- ISO_8859_5
- ISO_8859_6
- ISO_8859_7
- ISO_8859_8
- ISO_8859_9
- KOI8_R
- KOI8_U
- MACCENTEURO
- MACCROATIAN
- MACCYRILLIC
- MACGREEK
- MACICELAND
- MACJAPAN
- MACJAPANESE
- MACROMAN
- MACROMANIA
- MACTHAI
- MACTURKISH
- MACUKRAINE
- MacCentEuro
- MacCroatian
- MacCyrillic
- MacGreek
- MacIceland
- MacJapan
- MacJapanese
- MacRoman
- MacRomania
- MacThai
- MacTurkish
- MacUkraine
- PCK
- SHIFT_JIS
- SJIS
- SJIS_DOCOMO
- SJIS_DoCoMo
- SJIS_KDDI
- SJIS_SOFTBANK
- SJIS_SoftBank
- STATELESS_ISO_2022_JP
- STATELESS_ISO_2022_JP_KDDI
- Shift_JIS
- Stateless_ISO_2022_JP
- Stateless_ISO_2022_JP_KDDI
- TIS_620
- UCS_2BE
- UCS_4BE
- UCS_4LE
- US_ASCII
- UTF8_DOCOMO
- UTF8_DoCoMo
- UTF8_KDDI
- UTF8_MAC
- UTF8_SOFTBANK
- UTF8_SoftBank
- UTF_16
- UTF_16BE
- UTF_16LE
- UTF_32
- UTF_32BE
- UTF_32LE
- UTF_7
- UTF_8
- UTF_8_HFS
- UTF_8_MAC
- WINDOWS_1250
- WINDOWS_1251
- WINDOWS_1252
- WINDOWS_1253
- WINDOWS_1254
- WINDOWS_1255
- WINDOWS_1256
- WINDOWS_1257
- WINDOWS_1258
- WINDOWS_31J
- WINDOWS_874
- Windows_1250
- Windows_1251
- Windows_1252
- Windows_1253
- Windows_1254
- Windows_1255
- Windows_1256
- Windows_1257
- Windows_1258
- Windows_31J
- Windows_874
Public Class Methods
[T] (T) → T
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 30
def self._load: [T] (T) -> T
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 66
def self.aliases: () -> Hash[String, String]
Returns the hash of available encoding alias and original encoding name.
Encoding.aliases
#=> {"BINARY"=>"ASCII-8BIT", "ASCII"=>"US-ASCII", "ANSI_X3.4-1968"=>"US-ASCII",
"SJIS"=>"Windows-31J", "eucJP"=>"EUC-JP", "CP932"=>"Windows-31J"}
(untyped obj1, untyped obj2) → Encoding?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 91
def self.compatible?: (untyped obj1, untyped obj2) -> Encoding?
Checks the compatibility of two objects.
If the objects are both strings they are compatible when they are concatenatable. The encoding of the concatenated string will be returned if they are compatible, nil if they are not.
Encoding.compatible?("\xa1".force_encoding("iso-8859-1"), "b") #=> #<Encoding:ISO-8859-1> Encoding.compatible?( "\xa1".force_encoding("iso-8859-1"), "\xa1\xa1".force_encoding("euc-jp")) #=> nil
If the objects are non-strings their encodings are compatible when they have an encoding and: * Either encoding is US-ASCII compatible * One of the encodings is a 7-bit encoding
() → Encoding
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 121
def self.default_external: () -> Encoding
Returns default external encoding.
The default external encoding is used by default for strings created from the following locations:
-
Filedata read from disk -
SDBM
While strings created from these locations will have this encoding, the encoding may not be valid. Be sure to check String#valid_encoding?.
File data written to disk will be transcoded to the default external encoding when written, if default_internal is not nil.
The default external encoding is initialized by the -E option. If -E isn’t set, it is initialized to UTF-8 on Windows and the locale on other operating systems.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 135
def self.default_external=: (Encoding enc) -> Encoding
| [T < _ToStr] (T enc) -> T
Sets default external encoding. You should not set Encoding::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.
See Encoding::default_external for information on how the default external encoding is used.
() → Encoding?
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 166
def self.default_internal: () -> Encoding?
Returns default internal encoding. Strings will be transcoded to the default internal encoding in the following places if the default internal encoding is not nil:
-
Filedata read from disk -
Strings returned from Readline
-
Strings returned from SDBM
-
Values from ENV
-
Values in ARGV including $PROGRAM_NAME
Additionally String#encode and String#encode! use the default internal encoding if no encoding is given.
The script encoding (ENCODING), not default_internal, is used as the encoding of created strings.
Encoding::default_internal is initialized with -E option or nil otherwise.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 181
def self.default_internal=: (Encoding enc) -> Encoding
| [T < _ToStr] (T enc) -> T
| (nil) -> nil
Sets default internal encoding or removes default internal encoding when passed nil. You should not set Encoding::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.
See Encoding::default_internal for information on how the default internal encoding is used.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 213
def self.find: (encoding enc) -> Encoding?
Search the encoding with specified name. name should be a string.
Encoding.find("US-ASCII") #=> #<Encoding:US-ASCII>
Names which this method accept are encoding names and aliases including following special aliases
“external” : default external encoding
“internal” : default internal encoding
“locale” : locale encoding
“filesystem” : filesystem encoding
An ArgumentError is raised when no encoding with name. Only Encoding.find("internal") however returns nil when no encoding named “internal”, in other words, when Ruby has no default internal encoding.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 232
def self.list: () -> Array[Encoding]
Returns the list of loaded encodings.
Encoding.list #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>, #<Encoding:ISO-2022-JP (dummy)>] Encoding.find("US-ASCII") #=> #<Encoding:US-ASCII> Encoding.list #=> [#<Encoding:ASCII-8BIT>, #<Encoding:UTF-8>, #<Encoding:US-ASCII>, #<Encoding:ISO-2022-JP (dummy)>]
() → String
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 54
def self.locale_charmap: () -> String
Returns the locale charmap name. It returns nil if no appropriate information.
Debian GNU/Linux LANG=C Encoding.locale_charmap #=> "ANSI_X3.4-1968" LANG=ja_JP.EUC-JP Encoding.locale_charmap #=> "EUC-JP" SunOS 5 LANG=C Encoding.locale_charmap #=> "646" LANG=ja Encoding.locale_charmap #=> "eucJP"
The result is highly platform dependent. So Encoding.find(Encoding.locale_charmap) may cause an error. If you need some encoding object even for unknown locale, Encoding.find(“locale”) can be used.
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 246
def self.name_list: () -> Array[String]
Returns the list of available encoding names.
Encoding.name_list
#=> ["US-ASCII", "ASCII-8BIT", "UTF-8",
"ISO-8859-1", "Shift_JIS", "EUC-JP",
"Windows-31J",
"BINARY", "CP932", "eucJP"]
Public Instance Methods
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 257
def ascii_compatible?: () -> bool
Returns whether ASCII-compatible or not.
Encoding::UTF_8.ascii_compatible? #=> true Encoding::UTF_16BE.ascii_compatible? #=> false
() → bool
Source
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 270
def dummy?: () -> bool
Returns true for dummy encodings. A dummy encoding is an encoding for which character handling is not properly implemented. It is used for stateful encodings.
Encoding::ISO_2022_JP.dummy? #=> true Encoding::UTF_8.dummy? #=> false
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 281
def inspect: () -> String
Returns a string which represents the encoding for programmers.
Encoding::UTF_8.inspect #=> "#<Encoding:UTF-8>" Encoding::ISO_2022_JP.inspect #=> "#<Encoding:ISO-2022-JP (dummy)>"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 288
def name: () -> String
The name of the encoding.
Encoding::UTF_8.name #=> "UTF-8"
# File vendor/bundle/ruby/4.0.0/gems/rbs-4.0.3/core/encoding.rbs, line 298
def names: () -> Array[String]
Returns the list of name and aliases of the encoding.
Encoding::WINDOWS_31J.names #=> ["Windows-31J", "CP932", "csWindows31J", "SJIS", "PCK"]