Philip Hallstrom wrote: >> So it's good practice to use symbols to reference "things" in your >> program. This is why symbols make good keys in hashes since the keys >> in >> hashes are generally used to identify things in the hash and the >> actual >> characters are not as important. > > But, pay attention to the API for the library you are using. They > don't always accept a symbol -- some expect a string. They'll do > something like this:
Very good point. Thanks. > case passed_in_argument > when "foo" then ... > when "bar" then ... > end > > If you pass in :foo for the argument it won't match. > > Now that said, most of Rails runs stringify_keys on the arguments so > it's a non issue. Not all the plugins do. Just something to watch out > for if your symbol isn't doing what you expected it to do. > >> Now imagine what would happen if strings were used as keys: >> use >> the symbol :name the symbol would only take up memory once on its >> first >> usage. > > I thought I'd read somewhere though that a symbol once created is > never destroyed so it takes up space in RAM. A string will be > destroyed once it goes out of scope. The take away being that if you > create a thousand symbols but only use them once you're going to be > eating up a lot of RAM. An edge case maybe as typically you'll use the > symbol many times, but something to keep an eye on. > > Someone please correct me if I'm wrong about the symbol/ram/usage issue. Yes, I would expect that you are correct. As far as I know symbols "in essence" act like memory leaks. Unless there exists some mechanism in the garbage collector to deal with that. I suppose they would act like lazily loaded constants. Once allocated they would consume memory for the life-time of the application. 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

