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.
puts :blue.object_id a = "blue" b = "blue" puts a.object_id puts b.object_id # 913308 # 70109829938240 # 70109829938220
Recent Comments