object_id method returns the identity of an Object. If two objects have the same
object_id, they are the same (point to the same Object in memory).
Symbol with the same characters references the same Object in memory, and in case of String they’re referencing two different objects in memory. Whenever you use a new String, Ruby allocates memory for it.
If you’re in doubt whether to use a Symbol or a String, consider what’s more important: the identity of an object (i.e. a Hash key), or the contents (in the example above, “hello_world”).
Code snippet 📌
# Using Symbol ❯ :hello_world.object_id == :hello_world.object_id true # Using String ❯ "hello_world".object_id == "hello_world".object_id false