Skip to content

Instantly share code, notes, and snippets.

@tyfkda
Created February 4, 2016 00:04
Show Gist options
  • Save tyfkda/6be889dbd5c7c0392789 to your computer and use it in GitHub Desktop.
Save tyfkda/6be889dbd5c7c0392789 to your computer and use it in GitHub Desktop.
Convert string encoding
# Convert string encoding
# options:
# -e [encoding] Target encoding (default: utf-8)
require 'kconv'
require 'optparse'
def convert_encoding(lines, to)
encoding = Kconv.guess(lines.join)
lines.map do |line|
Kconv.kconv(line, to, encoding)
end
end
def main
params = ARGV.getopts('e:')
p params
to = params['e'] || 'utf-8'
lines = readlines
convert_encoding(lines, to).each do |line|
puts line
end
end
if $0 == __FILE__
main
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment