From: Robert Klemme Date: 2008-02-12T06:29:56+09:00 Subject: Re: Hash keys On 11.02.2008 20:32, J. Cooper wrote: > I ran into something I hadn't realized (common occurrence). Keying a > hash with a symbol is not the same as using a string. I guess it makes > sense, but I've seen it done both ways, and I had been always using > symbols for my keys. But then I ran into an issue on loading an external > YAML object into a hash, and didn't realize it was keyed with strings so > I got errors the first time around. > > So two questions: > 1) What is the preferred method of keying hashes? Symbols, strings, > other? It depends: my personal convention is this: use symbols if the set of keys is limited and probably known beforehand; use strings if the data is read from an external resource (e.g. a file) and there could be arbitrary key values. > 2) Is there a smooth way to handle hashes that may have been keyed in > either fashion? I do not think there is a smooth one size fits all way. You could of course convert a Hash containing on set of keys to the other one. I don't think it is worthwhile though and haven't seen it so far. Kind regards robert