From: Robert Klemme Date: 2005-03-17T06:34:46+09:00 Subject: Re: Symbol vs String "Nikolai Weibull" schrieb im Newsbeitrag news:20050316210032.GE5638@puritan.pcp.ath.cx... >* Sebesty�n G�bor (Mar 16, 2005 21:40): >> Just a dumb question: what is the real difference between { :aKey => >> "aValue" } and { "aKey" => "aValue" } ? I know the first key is a >> symbol the latter is a string. I like string keys why should I use >> symbols? Why symbols worth to use as keys? Thanks, > > Always use symbols for situations like these. The reason is that a > symbol is immutable and also that no new string needs to be created for > it if used more than once. Also, using strings as symbols and then > having the string altered will force a rehash of the table. It's all > about memory savings and execution speed, I rather make the distinction on the semantic level: for example, if you write an initializer for a class that accepts a hash to init any number of instance fields I'd prefer to use symbols here. Also, if there is only a certain fixed set of values allowed. I use strings if they are read from some source and I don't know beforehand, what they might be. Incidentally it's typical for the key like things to occur rather often, which fits nicely with the memory and speed savings incurred by symbols. Kind regards robert