Ruby: When to Use Symbol or String?

Symbols are just a way to name something. Typically it’s used to name attributes in a class or for options (colors, types of cuisines, type of shoe, etc). In essence, a constant string.

 

Use Strings instead of a Symbol when you need string like behavior (length, capitalize, reverse, etc). One thing to keep in mind is that Symbols are always in the same place in memory whereas Strings, even the same exact string, are in different parts of the heap.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *