So while a ruby String
is globally unique, a ruby Symbol
is not, because it's stored by id somewhere in the background:
http://thoughtsincomputation.com/posts/ruby-symbols-what-are-they
...which means using symbols use less memory, given that you have a specified set of values the symbol can be (you don't want to turn every string of user-entered text into a symbol for example).
My question is, while there is definitely a performance benefit of using symbols, is it worth it in the end? I'm asking because, for example, I write most of my hashes using symbols:
my_hash = {:_id => "123", :key => "value"}
But in the ruby-mongo-driver, all the keys are returned as strings:
mongo_hash = {"_id" => "123", "key" => "value"}
So if I were to use both my hash and the mongo hash the same way, I'd have to do:
mongo_hash = Model.collection.find_one(:key => "value")
#=> {"_id" => "123", "key" => "value"}
mongo_hash.symbolize_keys!
#=> {:_id => "123", :key => "value"}
But that just adds another layer to the application:
It seems like something's wrong there... either everything should be symbols or strings, there shouldn't be conversion back and forth, at least in this case.
What do you think about this? Is this okay? Is this even a problem, or is the ruby garbage collector and all that (haven't gone there yet) okay with this?
Unless you're seriously pushing the constraints of your server/system, the benefits or drawbacks of either method are going to be negligible.
When using a library that absolutely requires that you give it a string-keyed hash, it is obviously better to simply use strings, as it keeps your code clear and concise, and eliminates the need for you to cast the keys to strings.
Ruby aims to make programming more enjoyable for the developer, and makes no claim to be the most efficient. When I use Ruby, I take this to heart and use Symbols for the keys in my hashes simply because it makes them easier to read.
When it comes down to it, it's personal preference, and you won't notice a speed increase/decrease either way. Unless you're running into speed/memory constraint issues, you've got nothing to worry about. Other parts of the Ruby standard library will begin to fall apart before this becomes an issue.
"Premature optimization is the root of all evil" -- Donald Knuth
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With