Mehdi Farsi
Sep 2, 2018 · 1 min read

Hi :-)

I always think about a symbol as an identifier for a resource.

Because, that’s why Ruby uses them.

In effect, it allows to track and identify all the methods, variables, classes and modules of your running program via the global_symbols table.

This table contains all the symbols of your running program and it’s pre-loaded with all the Ruby resources as symbols.

It’s also used as an identifier for a resource because it’s fast to access it, and all the comparison operators use an object-level comparator (the comparison is made via the Object#object_id method).

So when you want to identify a resource (a state, a hash-key, etc..), it seems convenient and natural to prefer symbols to strings as Ruby already use them to identify resources.

Strings can also be used for identifying purposes. But they are slower than symbols for comparison and greedy in memory.

Hope that can help :-)

    Mehdi Farsi

    Written by

    Founder of www.rubycademy.com — I blog about Ruby & ROR - Paris