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

前端 未结 5 1754
-上瘾入骨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:51

    You can also write the YAML file that way:

    --- !map:HashWithIndifferentAccess
    one: 1
    two: 2
    

    after that:

    SETTINGS = YAML.load_file("path/to/yaml_file")
    SETTINGS[:one] # => 1
    SETTINGS['one'] # => 1
    

提交回复
热议问题