LoginSignup
5
4

More than 5 years have passed since last update.

Ruby 1.9 で標準入力から文字コードを変換する

Posted at

覚えているうちに脳内メモリを書き出しておく。例えば標準入力が Shift_JIS で出力を UTF-8 にしたい場合は以下のコードで出来る。

STDIN.set_encoding("Shift_JIS", "UTF-8").read

これはワンライナーで役に立つ。例えば Shift_JIS のテキストファイルから最初の一行を抽出してコンソールに書き出したい場合は以下になるだろう。

head -n sjis.txt | ruby1.9 -e 'p STDIN.set_encoding("Shift_JIS", "UTF-8").read'
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4