8.2. Symbols and their uses
Symbols are instances of the built-in Ruby class Symbol. They have a literal constructor: the leading colon. You can always recognize a symbol literal (and distinguish it from a string, a variable name, a method name, or anything else) by this token:
:a :book :"Here's how to make a symbol with spaces in it."
You can also create a symbol programmatically by calling the to_sym method (also known by the synonym intern) on a string, as you saw in the last section:
>> "a".to_sym => :a >> "Converting string to symbol with intern....".intern => :"Converting string to symbol with intern...."
Note the telltale leading colons on the evaluation results returned by irb.
You can easily convert a symbol to a string:
>> :a.to_s => ...
Get The Well-Grounded Rubyist, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.