Featured Video Play Icon

Alternative Ruby Symbol Literals

Periodically I take episodes from the RubyTapas archives and publish them for free. This episode from October 2012 is about symbol literals, and how you can use alternative quoting syntax to embed and interpolate any kind of character sequence in a symbol. And also: why you might not want to take advantage of this. Enjoy!

Video Script

As you know, a Symbol literal is a word with a colon in front of it.
You may also know that symbols aren’t limited to simple words; anything that can go in a String, can go in a symbol. But how do we get arbitrary characters, like spaces and dashes, into a symbol? One way is to start with a string and use <a href="https://www.rubytapas.com/out/ruby--string-to_sym">#to_sym</a> to convert it to a symbol:
"foo-bar (baz)".to_sym          # => :"foo-bar (baz)"
But there’s a more concise and idiomatic way to do it. If we precede a quoted string with a colon, it becomes a quoted symbol.
:"foo-bar (baz)"                # => :"foo-bar (baz)"
You can interpolate values into the quoted symbol just as you would into a string.
post_id = 123
:"post-#{post_id}"              # => :"post-123"

And it also works for single-quotes.
:'hello, "world"'               # => :"hello, \"world\""
Finally, if that doesn’t satisfy your symbol-quoting needs, there’s also a percent-quote literal syntax. Just like %q for strings, you can quote a symbol with %s followed by any delimiter you want.
%s{foo:bar}                     # => :"foo:bar"
Of course, all these different ways of writing symbols don’t mean you should go nuts with generating symbols. Symbols are best used for constrained vocabularies, like the names of options that may be passed to a method. The Ruby virtual machine has to keep a table in memory of all known symbols, so that it can assign a single unique integer ID to each. That means generating an unbounded set of symbols, from, say, user input can lead to memory leaks. So go easy on the symbol interpolation. That’s it for today. Happy hacking!


    1. My understanding is that symbols which are generated with .to_sym are garbage-collected (see Schneeman’s post on this). I’d be cautious about relying on symbol garbage collection for some of the other symbol quotation methods in this episode without doing some testing first.

      There is also no advantage (that I’m aware of) to using symbols over strings for unconstrained vocabularies. Symbols exist as an optimization for fast lookups, and that generally only matters for constrained vocabularies where the same string is used as a key over and over again (e.g. method calls, which is one of the things Ruby uses symbosl for internally). I certainly wouldn’t go out of my way to increase my use of symbols.

Leave a Reply

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