Created
February 4, 2016 00:04
-
-
Save tyfkda/6be889dbd5c7c0392789 to your computer and use it in GitHub Desktop.
Convert string encoding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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