LoginSignup
62
51

More than 5 years have passed since last update.

[Ruby]URLのクエリ文字列からHashを生成する

Last updated at Posted at 2014-10-20

RubyでURLのクエリ文字列をHashに変換する方法です。
標準添付ライブラリURIを利用します。
Ruby 2.1.0 リファレンスマニュアル > ライブラリ一覧 > uriライブラリ

require 'uri'

uri = URI::parse('http://qiita.com/search?utf8=%E2%9C%93&sort=rel&q=ruby')

q_array = URI::decode_www_form(uri.query)
#=> [["utf8", "✓"], ["sort", "rel"], ["q", "ruby"]]

q_hash = Hash[q_array]
#=> {"utf8"=>"✓", "sort"=>"rel", "q"=>"ruby"}

参考ページ

How to extract URL parameters from a URL with Ruby or Rails?

関連記事

Hash からクエリ文字列を生成する
Rubyで日本語のURI(URL)をパーズするなら Addressable::URI を使おう

62
51
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
62
51