2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シンボルハッシュを文字列ハッシュに変換するワンライナー

Last updated at Posted at 2015-11-30

railsで、コントローラに渡されたパラメータを、キーワード引数としてメソッドにそのまま渡そうとするとエラーになる。
ハッシュがのキーシンボルであるため、これらのキーを文字列に変換してやる必要がある。

hash_change
my_hash = my_hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}

参考
http://stackoverflow.com/questions/800122/best-way-to-convert-strings-to-symbols-in-hash

追記
railsであればハッシュに対してstringify_keys メソッドを使用するほうが簡単である。

2
1
3

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?