Data types: Symbol vs String

1,487 views
Skip to first unread message

Alistair

unread,
Jan 26, 2011, 6:38:09 PM1/26/11
to Mongoid
Hello,

I'm right at the start of a new project with Mongoid and I'm wondering
if there are any benefits or disadvantages to using Symbol data type
over String, and, what are the differences between them?

Thank you

--
Alistair

Khaja Minhajuddin

unread,
Jan 26, 2011, 11:44:45 PM1/26/11
to mon...@googlegroups.com
You should use a Symbol whenever a field can have a value from a finite set of values. e.g. If you are planning to store the "role" of a "user", it could be a symbol ( :admin, :customer, etc, ). FWIW Mongoid stores this as a "string" in mongodb as mongodb doesn't have any special datatype for Symbols. Mongoid transparently converts it from a String to Symbol while loading it and vice versa while saving it. So, it's only a symbol when loaded in your rails/ruby app, that means you get all the advantages of using symbols only in the rails/ruby context. This question on stackoverflow => http://stackoverflow.com/questions/2415780/why-dont-more-projects-use-ruby-symbols-instead-of-strings shows a few advantages of using Symbols.

Regards
Khaja Minhajuddin
http://cosmicvent.com

Alistair Holt

unread,
Jan 27, 2011, 11:01:22 AM1/27/11
to mon...@googlegroups.com
Thanks Khaja. It looks like it's mainly down to personal preference. Is there really an advantage to using Symbols over Strings in finite sets of values when you can specify validates_inclusion :something, :in => xxx for both?

--
Alistair

Khaja Minhajuddin

unread,
Jan 27, 2011, 11:22:27 PM1/27/11
to mon...@googlegroups.com
I can't think of anything other than the fact that, if we use symbols only one object is created in memory for one symbol, whereas with strings it creates as many objects as the number of times the string is used.

foo = :something
bar = :something
whatever = :something

<== at this point there is just one instance of :something in memory

foo = "chunky"
bar = "chunky"
baz = "chunky"

<== at this point there are 3 instances of "chunky" in memory.

This is how ruby behaves.

Regards
Khaja Minhajuddin
http://cosmicvent.com




Visnu Pitiyanuvath

unread,
Jan 28, 2011, 1:08:23 AM1/28/11
to mon...@googlegroups.com
there's also cost involved interning the symbol the first time. a good description of the differences and when to use them is at http://blog.hasmanythrough.com/2008/4/19/symbols-are-not-pretty-strings

Alistair Holt

unread,
Jan 28, 2011, 5:54:15 AM1/28/11
to mon...@googlegroups.com
Thanks guys. That's all really interesting :) Using the Symbol data type I guess there is also a slight cost involved in the string-to-symbol (and back again) conversion in Mongoid itself?

--
Alistair

Khaja Minhajuddin

unread,
Jan 28, 2011, 8:13:00 AM1/28/11
to Mongoid
In most cases where you would use a C# or Java enum, Symbols are
better than Strings. Can't say for sure what would be better without
knowing what you are using it for.
> > On Thu, Jan 27, 2011 at 9:31 PM, Alistair Holt <alistairh...@gmail.com>wrote:
>
> >> Thanks Khaja. It looks like it's mainly down to personal preference. Is
> >> there really an advantage to using Symbols over Strings in finite sets of
> >> values when you can specify validates_inclusion :something, :in => xxx for
> >> both?
>
> >> --
> >> Alistair
>
> >> On Jan 27, 2011, at 4:44, Khaja Minhajuddin <minhajud...@cosmicvent.com>
> >> wrote:
>
> >> You should use a Symbol whenever a field can have a value from a finite
> >> set of values. e.g. If you are planning to store the "role" of a "user", it
> >> could be a symbol ( :admin, :customer, etc, ). FWIW Mongoid stores this as a
> >> "string" in mongodb as mongodb doesn't have any special datatype for
> >> Symbols. Mongoid transparently converts it from a String to Symbol while
> >> loading it and vice versa while saving it. So, it's only a symbol when
> >> loaded in your rails/ruby app, that means you get all the advantages of
> >> using symbols only in the rails/ruby context. This question on stackoverflow
> >> => <http://stackoverflow.com/questions/2415780/why-dont-more-projects-use...>
> >>http://stackoverflow.com/questions/2415780/why-dont-more-projects-use...shows
> >> a few advantages of using Symbols.
>
> >> Regards
> >> Khaja Minhajuddin
> >> <http://cosmicvent.com/>http://cosmicvent.com

iEmpire

unread,
Feb 23, 2011, 4:09:51 AM2/23/11
to Mongoid
Which is better for the "gender" field: Symbol or String?

Alistair

unread,
Feb 23, 2011, 6:33:00 AM2/23/11
to mon...@googlegroups.com
Perhaps a boolean would be more suitable for a field with only two states like a gender field. i.e. male => true

Nick Hoffman

unread,
Feb 23, 2011, 11:19:29 AM2/23/11
to mon...@googlegroups.com
On Wednesday, February 23, 2011 4:09:51 AM UTC-5, iEmpire wrote:
Which is better for the "gender" field: Symbol or String?

Mongoid stores symbols as strings in MongoDB, so it boils down to whether symbols or strings suits your application better.

Kieran P

unread,
Feb 23, 2011, 2:19:06 PM2/23/11
to mon...@googlegroups.com
The devs of Diaspora would disagree with you there ;-)

Reply all
Reply to author
Forward
0 new messages