Encoding::UndefinedConversionError :: v.encode('utf-8', 'SJIS', invalid: :replace, undef: :replace, replace: ' ')

http://stackoverflow.com/questions/14305497/ruby-one-encoded-data-to-another-encoded-csv-with-many-lines

http://qa.atmarkit.co.jp/q/2349/revisions

文字列中に自身のエンコーディングとして不正な文字が入っていたり、変換先エンコーディングに変換できない文字が入っていた場合は例外が発生します。

# coding: utf-8
str = "\xff" # 0xFF は UTF-8 として不正な文字
str.encode('cp932') # "\xFF" on UTF-8 (Encoding::InvalidByteSequenceError)

# coding: utf-8
str = "\xe2\x99\xa1" # 0xE2 0x99 0xA1(ハートマーク)は CP932 には存在しない文字
str.encode('cp932') # U+2661 from UTF-8 to Windows-31J (Encoding::UndefinedConversionError)

オプションを指定して例外を発生させないようにでもきます。

# coding: utf-8
str = "あ\xffい"
# 変換元エンコーディングで不正な文字を置換する
str.encode('cp932', :invalid=>:replace) #=> CP932 で "あ?い"

# coding: utf-8
str = "あ\xe2\x99\xa1い" # 「あ(ハートマーク)い」
# 変換先エンコーディングに存在しない文字を置換する
str.encode('cp932', :undef=>:replace) #=> CP932 で "あ?い"