Reading filename in multiple OS without encoding problem with Ruby

核能气质少年 提交于 2019-12-01 23:26:11

I do this on Ruby 1.9 when I want to make sure things are in UTF-8:

if filename.encoding.to_s != 'UTF-8'
  filename.encode!('UTF-8')
end

Unless your OS gives a file name encoded with a coding system which doesn't support some special characters found on the file name, it can be encoded to UTF-8 without a hickup.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!