Ruby 2.3.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Encoding::Converterクラス > new

singleton method Encoding::Converter.new

new(source_encoding, destination_encoding) -> Encoding::Converter[permalink][rdoc]
new(source_encoding, destination_encoding, options) -> Encoding::Converter
new(convpath) -> Encoding::Converter

Encoding::Converter オブジェクトを作成します。

[PARAM] source_encoding:
変換元のエンコーディング
[PARAM] destination_encoding:
変換先のエンコーディング
[PARAM] options:
変換の詳細を指定する定数やハッシュ
[PARAM] convpath:
変換経路の配列

options では String#encode でのハッシュオプションに加えて、以下の定数が利用可能です。


# UTF-16BE to UTF-8
ec = Encoding::Converter.new("UTF-16BE", "UTF-8")

# Usually, decorators such as newline conversion are inserted last.
ec = Encoding::Converter.new("UTF-16BE", "UTF-8", :universal_newline => true)
p ec.convpath #=> [[#<Encoding:UTF-16BE>, #<Encoding:UTF-8>],
              #    "universal_newline"]

# But, if the last encoding is ASCII incompatible,
# decorators are inserted before the last conversion.
ec = Encoding::Converter.new("UTF-8", "UTF-16BE", :crlf_newline => true)
p ec.convpath #=> ["crlf_newline",
              #    [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]

# Conversion path can be specified directly.
ec = Encoding::Converter.new(["universal_newline", ["EUC-JP", "UTF-8"], ["UTF-8", "UTF-16BE"]])
p ec.convpath #=> ["universal_newline",
              #    [#<Encoding:EUC-JP>, #<Encoding:UTF-8>],
              #    [#<Encoding:UTF-8>, #<Encoding:UTF-16BE>]]