3 ms·
You can dynamically create symbols and expect them to be garbage collected as of Ruby 2.2. They only become "immortal" if you use them to define a method, insta
by djur 4y ago
You can dynamically create symbols and expect them to be garbage collected as of Ruby 2.2. They only become "immortal" if you use them to define a method, instance variable, or constant. So the common use case of hash keys is fine. One important advantage of symbols versus strings is that Symbol#== is constant time.
- inferiorhuman 4y agoAh hah, you've discovered how long it's been since I've thought about Ruby performance. One important advantage of symbols versus strings is that Symbol#== is constant time. At that point though why not use integer constants?
- knodi123 4y agosyntactic sugar- you don't have to pre-declare them or worry about conflicting values. And occasionally the symbol.to_s method is useful in logging or whatnot. That's pretty much it.