Freeze String Literal for Ruby on Rails
Something that always bothered me was rubocop asking to add
# frozen_string_literal: true
to my ruby files. I always ended up removing the
option on rubocop configuration.
# Don't do it, unless the memory usage doesn't matter!!!
Style/FrozenStringLiteralComment:
Enabled: false
It turns out that this comment can save a lot of memory on your running projects. This option avoids the same string being allocated many times in the memory.
To add the magic comment to the beginning of all ruby files, simply run this command:
# You must have the gem rubocop installed
bundle exec rubocop --auto-correct --only FrozenStringLiteralComment
bundle exec rubocop --auto-correct --only Layout/EmptyLineAfterMagicComment
The second command is to add a blank line after the magic comment. I’m not sure
how to run two rubocop --only
at once, so I prefer to run the two commands.
The result of this was that I was receiving notifications about the high memory usage, and after that, the project never had a memory issue again.
References:
Comments
comments powered by Disqus