Ruby symbols are so regularly used but are mystic to the amatuer developer. There’re several explanations what they are and how they should be used. To me, they are String constants, whose value is the name of the constant itself. Therefore :something contains the value “something” and cannot be changed.

This is preferred over using “something” in Ruby directly because unlike Java it doesn’t have a String pool and using “something” over and over creates separate String objects in memory. As such, symbols are useful if a particular String is repeatedly used in your application. Now go read again about the confusing stuff about being able to to_i and being an instance of a Symbol class. It should make a little bit more sense.

RSS feed for comments on this post · TrackBack URL

Leave a Comment