On Jul 22, 2006, at 12:50 PM, Wes Gamble wrote:

> All,
>
> I have a Rails app where I load a YAML file into a class variable  
> (in my
> application.rb file so it's available to all controllers), like so:
>
>   @@config = YAML.load_file("#{RAILS_ROOT}/config/eSimplyConf.yml")
>
> and I created a method to give me back this hash:
>
>   def config
>     @@config
>   end
>
> However, I notice that when I call config[:key_name], I don't get
> anything, but when I call config['key_name'] I do get the value.  I
> further verified that if I called config[:key_name.to_s], I will  
> get the
> value, thus underscoring the distinction between symbol and string  
> here
> as the type of key_name.
>
> I was under the impression that symbols and strings were  
> interchangeable
> for the purpose of addressing a hash element.  Is this not the  
> case?  Is
> my problem that YAML.load_file generates keys that are strings instead
> of symbols, and of course, I'm using Rails, so that I expect all of my
> hash keys to be symbols :)?
>
> Thanks,
> Wes
>
> -- 
> Posted via http://www.ruby-forum.com/.
>


Wes-

	The symbol and string as hash key are specific to rails  
implementation of HashWithIndifferetnAccess. Thi is a hash that  
responds to string and symbol keys in the same way. This is not the  
behavior of the standard ruby Hash class. If you want this behavior  
then make your config hash be a HashWithIndifferentAccess instead of  
a normal hash. You can do this by changinf the header in your yaml  
file to this:

--- !map:HashWithIndifferentAccess
foo: bar
bar: baz


-Ezra