If you have played around with YAML files in Rails at all, you may have run across a confusing issue: often times in Rails, hashes are accessed via either a string or symbol but if you load up a YAML file, the hash can only be accessed by string!
The reason is Rails has a "HashWithIndifferentAccess" object type, that is built on top of the standard ruby Hash, and YAML files by default use the standard ruby hash.
E.g.
You can get the indifferenct access behavior in YAML files by putting this at the top of your YAML file:
The reason is Rails has a "HashWithIndifferentAccess" object type, that is built on top of the standard ruby Hash, and YAML files by default use the standard ruby hash.
E.g.
h = { :msg => 'Hello World', :date => Time.now }
puts h[:msg] # Hello World
puts h['msg'] # nil
h = HashWithIndifferentAccess.new( { :msg => 'Hello World', :date => Time.now } )
puts h[:msg] # Hello World
puts h['msg'] # Hello World
You can get the indifferenct access behavior in YAML files by putting this at the top of your YAML file: