Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable the Ruby 2.3 `--enable-frozen-string-literal` globally in Rails?

I am building a greenfield Rails application on top of Ruby 2.3, and I would like all Rails commands (e.g. rails s, rails c) and all Ruby commands (e.g. rake do:something) to use the new immutable-String functionality introduced in Ruby 2.3. (See, e.g. https://wyeworks.com/blog/2015/12/1/immutable-strings-in-ruby-2-dot-3/)

So, how do I pass that lovely --enable-frozen-string-literal Ruby option down to Ruby in all possible contexts where some command I issue bottoms out in Ruby?

Thanks in advance!

like image 298
aec Avatar asked Jan 23 '16 02:01

aec


People also ask

How do I fix missing frozen string literal comment?

The property can be found here: FrozenStringLiteral. This change just silences the warning. The linked page describes the right solution: Add the frozen_string_literal comment to the top of files to help transition to frozen string literals by default. .

What is frozen string literal in rails?

By John Cinnamond on February 23, 2022 Among the many changes you can expect from this version is the confusingly named “Frozen String Literal Pragma.” This feature lets you add a magic comment to your Ruby source code, which makes all string literals frozen by default—that is, you won't be able to modify them.

What does it mean that strings are immutable in Ruby?

An immutable object is an object whose state cannot be modified after it is created. As immutable objects state cannot be changed it doesn't make sense to copy the same object over and over. In fact, Ruby 2.3 (with frozen strings enabled) will hold a unique instance for each string literal value used in the program.

Is string a literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string.


1 Answers

As far as I know, best way to do it is setting environment variable like followings:

export RUBYOPT=--enable-frozen-string-literal

or

setenv RUBYOPT --enable-frozen-string-literal

However, don't try it right now. It simply doesn't work because some codes in Bundler gem are attempting to modify frozen String. Wait until they fix the problem.

like image 78
Sa-ryong Kang Avatar answered Sep 18 '22 14:09

Sa-ryong Kang