32
19

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.

HashのキーがSymbolでもStringでも値を取得する

Last updated at Posted at 2016-01-09

通常、RubyはHashのキーがSymbolかStringかを判別する

hash = { :key => 'sym', 'key' => 'str' }

p hash[:key]
# => 'sym'

p hash['key']
# => 'str'

しかし、Railsの、例えばcontrollerなどで使われるparamsではこうはならない。SymbolとStringのどちらを指定しても同じ値が取得できる。

class SampleController < ApplicationController
  def index
    p params[:controller]  # => 'sample'
    p params['controller'] # => 'sample'
  end
end

これはparamsがただのHashではなく、active_supportのHashWithIndifferentAccessクラスを継承したクラスであるため(ActionController::Parameters)。

HashWithIndifferentAccessクラス

なのでこのクラスを使えばRailsのparamsみたいにキーがsymbolでもstringでも同じ値を取得することができるようになる。

Hashクラスに拡張された、with_indifferent_accessメソッドを呼び出して使った例が次のコード。

Hash#with_indifferent_access

require 'active_support'
require 'active_support/core_ext'

hash = { :key => 'value' }.with_indifferent_access
p hash[:key]
# => 'value'

p hash['key']
# => 'value'

HashWithIndifferentAccessクラスをnewして使うことも可能

require 'active_support'
require 'active_support/core_ext'

hash = ActiveSupport::HashWithIndifferentAccess.new(key: 'value')
p hash[:key]
# => 'value'

p hash['key']
# => 'value'

実装的には、HashWithIndifferentAccess#convert_keyを使って、keyがsymbolの場合に文字列化しているだけっぽかった。

def convert_key(key)
  key.kind_of?(Symbol) ? key.to_s : key
end
32
19
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
32
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?