別のキーワード
検索結果
-
Encoding
:: CompatibilityError (18018.0) -
2つのエンコーディング間に互換性がない場合に発生する例外。
...い場合に発生する例外。
エンコーディングの異なる文字列を連結しようとした場合などに発生します。
//emlist[例][ruby]{
"あ".encode("EUC-JP") + "あ".encode("UTF-8")
#=> Encoding::CompatibilityError: incompatible character encodings: EUC-JP and UTF-8
//}... -
String (60.0)
-
文字列のクラスです。 ヌル文字を含む任意のバイト列を扱うことができます。 文字列の長さにはメモリ容量以外の制限はありません。
...ィングに従い、1バイトではなく1文字を単位として動作します。
エンコーディングの変換にはメソッド String#encode を使います。
//emlist[例:エンコーディングの変換][ruby]{
p "いろは".size #=> 3
p "漢字"[0] #=> "漢"
p "山本......すね)
p "ループ".reverse #=> "プール"
s = "ruビー"
s[0..1] = "ル"
p s #=> "ルビー"
e = "言語".encode("EUC-JP")
u = "言語".encode("UTF-8")
p e.encoding #=> Encoding::EUC_JP
p u.encoding #=> Encoding::UTF_8
//}
より詳しく......合][ruby]{
s = "いろは"
a = s.encode("EUC-JP")
b = s.encode("UTF-8")
p a == b #=> false
s = "合".encode("EUC-JP")
p s + "\u{4f53}".encode("EUC-JP") #=> "合体"
p s + "\u{4f53}" #=> Encoding::CompatibilityError
//}
String#eql? はハッシュの... -
多言語化 (42.0)
-
多言語化 Ruby は US-ASCII はもちろん、US-ASCII 以外の文字エンコーディングもサポートしています。 文字列の内部表現のエンコーディングは固定されておらず、 プログラマは目的に応じて使用するエンコーディングを選ぶことができます。
...エンコーディングの変更
文字列のエンコーディングを変更するには、次の2つのメソッドを用います。
String#encode メソッドは文字列のエンコーディングを変換した新しい文字列を生成して返します。
Ruby 1.9 ではこれを用い......から読み込んだ文字列に後からエンコーディングを
設定する場合などに使います。
//emlist[例][ruby]{
"いろは".encode("Shift_JIS")
"\xA4\xA4\xA4\xED\xA4\xCF".force_encoding("EUC-JP")
//}
====[a:ruby18] Ruby 1.8 からの移行
Ruby 1.8 からの移行措置と......の結合ができません。
例外 (Encoding::CompatibilityError) が発生します。
//emlist[][ruby]{
p Encoding::ISO_2022_JP.dummy? # => true
s = "漢字".encode("ISO-2022-JP")
p s[0] #=> "\e"
s + "b" #=> Encoding::CompatibilityError: incompatible character encodings: ISO-2022-JP and... -
正規表現 (18.0)
-
正規表現 * metachar * expansion * char * anychar * string * str * quantifier * capture * grouping * subexp * selector * anchor * cond * option * encoding * comment * free_format_mode * absenceop * list * specialvar * references
...ォルトは UTF-8
/foo\b/.match("あいうfoo%") # => #<MatchData "foo">
/\bfoo\b/.match("あいうfoo%") # => nil
/\bfoo\b/e.match("(foo)".encode("EUC-JP")) # => nil
/\bfoo\b/.match("(foo)") # => #<MatchData "foo">
//}
Unicode の規格では、単語を成す文字を Word というプ......<Encoding:UTF-8>
//}
正規表現のエンコーディングと文字列のエンコーディングが非互換で
ある場合、Encoding::CompatibilityError が発生します。
エンコーディングについては spec/m17n も参考にしてください。
Regexp#fixed_encoding? で正規......う" # => nil
/abc/e =~ "あいう"
# ~> -:6:in `<main>': incompatible encoding regexp match (EUC-JP regexp with UTF-8 string) (Encoding::CompatibilityError)
//}
===[a:comment] コメント
(?#comment here) という記法で正規表現内にコメントを書くことができます。
この...