以下の内容はhttps://takuya-1st.hatenablog.jp/entry/2016/08/31/171408より取得しました。


ruby の URI で日本語を含むURIに対応する。

日本語URIを扱うという戦い

rubyURIで日本語を含む文字列が扱えなくなったので、何とかしないといけないとアレコレ模索した。

>> URI.parse  'http://example.com/あああ'
URI::InvalidURIError: URI must be ascii only "http://example.com/\u{3042}\u{3042}\u{3042}"
from /Users/takuya/.rbenv/versions/2.2.3/lib/ruby/2.2.0/uri/rfc3986_parser.rb:20:in `split'

URI.encode が使えることを知った。

>> URI.encode 'http://example.com/あああ'
=> "http://example.com/%E3%81%82%E3%81%82%E3%81%

parse encode と合わせて使うことでほとんどの問題は解決する。

>> URI.parse URI.encode 'http://example.com/あああ'
=> #<URI::HTTP http://example.com/%E3%81%82%E3%81%82%E3%81%82>

これで、エラーなく扱うことができる。

が、、、、encode を毎回指定するのはめんどくさくないかな。

参考資料

http://d.hatena.ne.jp/haroperi/20110405/1301959688




以上の内容はhttps://takuya-1st.hatenablog.jp/entry/2016/08/31/171408より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14