DEV Community

[Comment from a deleted post]
Collapse
 
ben profile image
Ben Halpern

Every time you say :rose you are referring to literally the same flower. Every "rose" might look and feel the same, but it is not the same flower.

In a lot of cases it doesn't matter whether it's the same or a copy but if you have to gather up all the roses in the garden. The symbols can fit in your hand. The strings might take up a whole shipping container.

A symbol of name :rose is literally the same object as every other symbol :rose in the application.

irb(main):007:0> :rose.object_id
=> 1146588
irb(main):008:0> :rose.object_id
=> 1146588
irb(main):009:0> :rose.object_id
=> 1146588

The string "rose" on the other hand is new every time.

irb(main):010:0> "rose".object_id
=> -70361110657240
irb(main):011:0> "rose".object_id
=> -70361110657240
irb(main):012:0> "rose".object_id
=> -70361110657240

Symbols are immutable which can have a positive impact on your application's speed and reliability. Once a symbol is declared, it's value is constant.

Hope that helps.

Collapse
 
tadman profile image
Scott Tadman • Edited

I know you're trying to prove that strings are different each time, but in your example they're all the same.

Did you have the "frozen string literal" thing still turned on?