On Tue, Sep 8, 2009 at 5:03 PM, Frederick
Cheung<[email protected]> wrote:
>
>
> On Sep 8, 8:49 pm, Robert Walker <[email protected]>
> wrote:
>> Philip Hallstrom wrote:
>>
>> As it goes with many things in life there are trade-offs, and finding
>> the right balance is often complex. There is overhead in constantly
>> creating and destroying objects, but keeping them around consumes
>> memory. Also keep in mind that symbols are stored internally as
>> integers, hence making comparisons significantly more efficient than
>> comparing strings.
>
> Another reason why they make good hash keys. There is a good post on
> symbols at 
> http://blog.hasmanythrough.com/2008/4/19/symbols-are-not-pretty-strings
>
> Fred

On the other hand.

The HashWithIndifferentAccess which ActiveSupport adds to Ruby and is
used extensively by Rails doesn't actually use symbols as the hash
keys.

Although it allows symbols and strings to be used interchangeably as
key arguments to the Hash methods. It converts symbiols to strings
before actually using them as keys, rather than the other way around,
which simply allows keystrokes to be saved in source code :str instead
of 'str'.

The reason it does it this way is that the keys often come in the form
of strings, and those strings come from outside the app, for example
when url parameters get converted to keys in the params hash.  Since
converting strings to symbols makes a copy of the string the first
time that particular string gets interned as a symbol, and those can
never be garbage collected, strings rather than symbols are used as
the keys.


-- 
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to