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

1

16

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!

Broyles answered 23/1, 2016 at 2:4 Comment(0)
M
24

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.

Manvel answered 1/2, 2016 at 3:59 Comment(6)
Bundler should be able to support frozen string literals since v1.12. If it still doesn't work, file an issue like this.Scaleboard
More portable: RUBYOPT=--enable-frozen-string-literal; export RUBYOPT will work with /bin/sh on FreeBSD, bash, zsh, fish, etc.Napoleon
what about if gems are trying to modify strings? It's probably not common but I'm sure it happens somewheere.Coronal
Definitely not ready for production :) There was an error while trying to load the gem 'haml'. (Bundler::GemRequireError) Gem Load Error is: can't modify frozen StringPuseyism
Not even Rails v5.0.2 handles it: a rather fresh app errors with …actionview-5.0.2/lib/action_view/helpers/javascript_helper.rb:16:in 'force_encoding': can't modify frozen String (RuntimeError) on startup.Brasilein
rails 5.2 fails for me too :/Mazurek

© 2022 - 2024 — McMap. All rights reserved.