Skip to content

Instantly share code, notes, and snippets.

@hadashiA
Created June 29, 2012 06:42
Show Gist options
  • Save hadashiA/3016299 to your computer and use it in GitHub Desktop.
Save hadashiA/3016299 to your computer and use it in GitHub Desktop.
Rubyで文字ード変換、未定義文字のみnkfをかます
# nkfはデフォルトで半角カナを全角カナにしてくれたり、未定義文字を良い感じにしてくれるので、String#encodeで変換できなかった文字はnkfに渡す。
# -xを渡すと、半角全角変換はしない。
str.encode('EUC-JP', Kconv.guess(str), fallback: -> char { NKF.nkf('-ex', char) })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment