Skip to content

Instantly share code, notes, and snippets.

@matsuda
Created March 15, 2019 09:59
Show Gist options
  • Save matsuda/1abfe977f39d81f23ea9df903ed8ceb1 to your computer and use it in GitHub Desktop.
Save matsuda/1abfe977f39d81f23ea9df903ed8ceb1 to your computer and use it in GitHub Desktop.
Ruby文字列のUnicodeエスケープシーケンスをデコードする
#
# 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