Created
March 15, 2019 09:59
-
-
Save matsuda/1abfe977f39d81f23ea9df903ed8ceb1 to your computer and use it in GitHub Desktop.
Ruby文字列のUnicodeエスケープシーケンスをデコードする
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
# | |
# https://techracho.bpsinc.jp/baba/2013_05_31/8837 | |
# | |
dir = '~/Downloads/Dev/JSON/' | |
dir = File.expand_path(dir) | |
def decode(file) | |
puts file | |
result = "" | |
File.open(file) do |f| | |
result = f.read | |
end | |
result = result.gsub(/\\u([\da-fA-F]{4})/) { [$1].pack('H*').unpack('n*').pack('U*') } | |
puts result | |
File.open(file, 'w') do |f| | |
f.write result | |
end | |
end | |
# file = 'xxx.json' | |
# path = File.join(dir, file) | |
# decode(path) | |
Dir.glob("#{dir}/*") do |file| | |
decode file | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment