If I have a hash in Ruby on Rails, is there a way to make it indifferent access?

前端 未结 5 1750
-上瘾入骨i
-上瘾入骨i 2021-02-01 12:04

If I already have a hash, can I make it so that

h[:foo]
h[\'foo\']

are the same? (is this called indifferent access?)

The details: I

5条回答
  •  温柔的废话
    2021-02-01 12:48

    Use HashWithIndifferentAccess instead of normal Hash.

    For completeness, write:

    SETTINGS = HashWithIndifferentAccess.new(YAML.load_file("#{RAILS_ROOT}/config/settings.yml"­))
    

提交回复
热议问题