And symbols can’t be changed, so if you want to work with the individual characters of the symbol then you want a string.
You can do this using the to_s method.
For example, when using method_missing you get the name of the missing method as a symbol. You may want to check if this method name matches a certain pattern (like ending in ?).
Example:
def method_missing(method_name, *args, &block)
if method_name.to_s[-1] == "?"
# do something
else
super
end
end
You can also convert a string object into a symbol object.
The method to do this is String#to_sym:
"rubyguides".to_sym
:rubyguides
Creating An Array Of Symbols
If you want to create an array of symbols you can use this code:
symbols = %i(a b c)
[:a, :b, :c]
This saves you from having to type the colons & the commas.
Similar to the string version %w:
strings = %w(a b c)
["a", "b", "c"]
Ruby Symbols Video
Symbol GC (Advanced)
Another interesting fact about symbols is that there are different types.
The reason is that symbols were not garbage collected before Ruby 2.2, which means that they where not cleaned up from memory when no longer needed like regular Ruby objects (strings, hashes, arrays…).
You can see an example here:
p Symbol.all_symbols.size
# 2443
('aa'..'aj').map(&:to_sym)
GC.start
p Symbol.all_symbols.size
# 2453
You will notice that the total count of symbols increases by 10, just like you would expect since we are creating 10 new symbols.
But since Ruby 2.2 these symbols are removed from memory because they are just temporary & not being used by something else in this code.
If you try this code on a version of Ruby that has Symbol GC enabled both symbol counts will be the same.
Some symbols will never be removed from memory, these are called “immortal symbols”.
Notice that symbols created directly, like :a1 will automatically become immortal symbols. Creating a new method will also create an immortal_static_symbol to go with it.
So where do mortal symbols come from?
From strings converted into symbols with the to_sym method.
You can check this yourself using ObjectSpace.count_symbols.
And if you are wondering what’s an immortal_dynamic_symbol, it’s a symbol that has been promoted from mortal to immortal. This can happen when you create a method with the name of a mortal symbol.
Summary
On this article you learned:
Symbols are immutable
Symbols are not pointer to values, they are values themselves
Strings are for data & symbols are for identity
How to convert between strings & symbols
Symbol GC was introduced in Ruby 2.2 to clean up temporary symbols
Hope you learned something new!
Please share this post so it can reach more people 🙂
Related
4 comments
Dmytro says
5 years ago
Great work Jesus, i find your posts helpful & enjoyable 🙂