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!

aec
  • 833
  • 1
  • 9
  • 18

1 Answers1

23

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.

Sa-ryong Kang
  • 286
  • 2
  • 3
  • Bundler should be able to support frozen string literals [since v1.12](http://bundler.io/v1.12/whats_new.html). If it still doesn't work, file an issue like [this](https://github.com/bundler/bundler/issues/4520). – Franklin Yu Sep 13 '16 at 16:02
  • More portable: `RUBYOPT=--enable-frozen-string-literal; export RUBYOPT` will work with `/bin/sh` on FreeBSD, bash, zsh, fish, etc. –  Oct 22 '16 at 22:37
  • what about if gems are trying to modify strings? It's probably not common but I'm sure it happens somewheere. – max pleaner Nov 01 '16 at 17:09
  • 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 String` – Dorian Nov 02 '16 at 20:50
  • 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. – Carsten Mar 09 '17 at 10:24
  • rails 5.2 fails for me too :/ – graywolf Sep 08 '18 at 16:05