28

We can easily find such style from lots of famous repositories, like rack, rails, etc.

For example in rack:

PATH_INFO      = 'PATH_INFO'.freeze
REQUEST_METHOD = 'REQUEST_METHOD'.freeze
SCRIPT_NAME    = 'SCRIPT_NAME'.freeze
QUERY_STRING   = 'QUERY_STRING'.freeze
CACHE_CONTROL  = 'Cache-Control'.freeze
CONTENT_LENGTH = 'Content-Length'.freeze
CONTENT_TYPE   = 'Content-Type'.freeze

Another examle in rails:

HTTP_IF_MODIFIED_SINCE = 'HTTP_IF_MODIFIED_SINCE'.freeze
HTTP_IF_NONE_MATCH     = 'HTTP_IF_NONE_MATCH'.freeze
HTTP_IF_NONE_MATCH     = 'HTTP_IF_NONE_MATCH'.freeze

I wonder why these constant strings are frozen. Since they are all constants, there should be only one instance. Of course we can put "foo".freeze somewhere to reference the same singleton instance, however people usually write literal variable name like HTTP_IF_MODIFIED_SINCEinstead.

So in my opinion, it doesn't make any difference in spite of using #freeze, so why do people freeze constants?

1
  • 5
    To stop people mutating them by accident? Dec 30, 2014 at 9:20

3 Answers 3

40

It is correct that Ruby prints a warning when you re-assign a value to an already initialized constant:

FOO = 'foo'
FOO = 'bar'
# :2: warning: already initialized constant FOO
# :1: warning: previous definition of FOO was here
FOO
#=> "bar"

But there is no protection from changing parts of the value of the constant. Example without freeze:

FOO = 'foo'
FOO[1] = '-'
FOO
#=> "f-o"

But freeze allows protecting the value of the constants from being changed. Example with freeze:

FOO = 'foo'.freeze
FOO[1] = '-'
#=> RuntimeError: can't modify frozen String
2
  • 1
    Thank you for your answer! It's ridiculous that I remember the magic effect of String#freeze, but forget the original intention of #freeze...Orz Dec 30, 2014 at 9:40
  • 4
    I believe the main reason in Rack and Rails is performance related. See this article from Richard Schneeman regarding optimizations from using String#freeze Dec 31, 2014 at 5:12
8

normally Rubyist freeze string literals to make execution faster. If there is some function call for example like below in some controller, every request will call that function.

log("debug")

what happens is ruby defines a new garbage string object every time. Object allocation is not free. it consumes memory and CPU. Garbage will be there till GC collects them.

but if literals are frozen

log("debug".freeze)

ruby allocates once and caches it for later use. Also, the string object will be immutable and safe for use in the multithreaded environment.

From ruby 3.0 ruby is gonna freeze every string, - according to Matz.


update:

If you add the following comment in the beginning of ruby file then every string literal in the entire file will be immutable. This is pretty helpful when you are trying to optimize your app for the muti-threaded environment.

# frozen_string_literal: true

or you can even start your Ruby process with --enable-frozen-string-literal switch.

1

One explanation why you see this consistent freezing of constants in popular projects is that they use Rubocop, a code analyzer.

It's a standard Rubocop rule that constants shouldn't be mutable, for the reasons mentioned above by @spickermann.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.