Ruby 4.1 リファレンスマニュアル

instance method String#encode!

encode!(encoding, options = nil) -> selfRuby 1.9.3 から[permalink][rdoc][edit]
encode!(encoding, from_encoding, options = nil) -> self
(?encoding source_encoding, ?encoding? from_encoding, ?invalid: :replace ?, ?undef: :replace ?, ?replace: string?, ?xml: (:text | :attr)?, ?newline: (:universal | :crlf | :cr | :lf)?, ?universal_newline: boolish, ?cr_newline: boolish, ?crlf_newline: boolish, ?lf_newline: boolish, ?fallback: ^(String) → string? | Method | _EncodeFallbackAref) → self

self を指定したエンコーディングに変換し、自身を置き換えます。引数を2つ与えた場合、第二引数は変換元のエンコーディングを意味します。そうでない場合は self のエンコーディングが使われます。変換後の self を返します。

(gsub!などと異なり)変換が行なわれなくても self を返します。

[PARAM] encoding:
変換先のエンコーディングを表す文字列か Encoding オブジェクトを指定します。
[PARAM] from_encoding:
変換元のエンコーディングを表す文字列か Encoding オブジェクトを指定します。
[RETURN]
変換後のself
#coding:UTF-8
s = "いろは"
s.encode!("EUC-JP")
s.encode!(Encoding::UTF_8)

[SEE_ALSO] String#encode