'# frozen_string_literal: true' is really needed in a rails application??? do i need to put it in each file?
Asked Answered
M

1

10

Every file of my rails app i'm editing displays this warning, missing frozen string literal comment, i know this is used to reserve memory and avoid memory re-allocation for all strings literal, but is it necessary to use on rails or its enabled by default? if so, how to enable it globally?

Masqat answered 11/8, 2021 at 17:59 Comment(2)
please, mention ruby and rails versions of the application.Adriene
ruby 2.5.9 & rails 5.1.8Masqat
S
13

Nope, it's not enabled by default.

However, you may use Rubocop to append it to the top of your files with Rubocop::Cop::Style::FrozenStringLiteralComment. It's an auto-correctable offense.

According to Holger Just:

You can actually enable it globally by invoking the ruby interpreter with ruby --enable=frozen-string-literal. However, this is usually a bad idea and will break in various subtle ways unless you are very sure that all files in all your gems and dependencies actually expect frozen literals (which is generally not the case)

Slinky answered 11/8, 2021 at 18:35 Comment(5)
You can actually enable it globally by invoking the ruby interpreter with ruby --enable=frozen-string-literal. However, this is usually a bad idea and will break in various subtle ways unless you are very sure that all files in all your gems and dependencies actually expect frozen literals (which is generally not the case)Chucklehead
Glad to know this! I will include it to my answerConaway
@HolgerJust is there any command flags where i could add it only to my project classes???Masqat
@ElissonG.M.Silva No, Ruby does not work that way. However, Rubocop has a feature to add the comment at the top to every one of your files which you can run e.g. before each release. See Rubocop's documentation and the link in the answer above.Chucklehead
You can use rubocop to auto-add this magic comment but your app will fail. So, be careful and ensure you have an adequate amount of test coverage.Oscan

© 2022 - 2024 — McMap. All rights reserved.