fastruby / fast-ruby

:dash: Writing Fast Ruby :heart_eyes: -- Collect Common Ruby idioms.
https://github.com/fastruby/fast-ruby
5.67k stars 376 forks source link

Hash fetch with symbols vs strings. #83

Closed ixti closed 8 years ago

ixti commented 8 years ago

Description is not really correct. Difference between :symbol key and "string" keys is that Symbols are immutable constants, while in case of using a String key you creating that string over and over again. Starting with Ruby 2.1, you can mark string as frozen and it will be created only once:

10.times.map { "foobar".object_id }.uniq.count # => 10
10.times.map { "foobar".freeze.object_id }.uniq.count # => 10

See:

ixti commented 8 years ago

Oh. I'm sorry. Didn't read article mentioned in description to the bottom. It actually mentions .freeze and some more info.

ixti commented 8 years ago

Note for myself: read linked articles from the top to the bottom before giving any advices :D

JuanitoFatas commented 8 years ago

Oh. I'm sorry. Didn't read article mentioned in description to the bottom. It actually mentions .freeze and some more info.

Not at all! Thanks for opening this issue.