What does the comment "frozen_string_literal: true" do?

comment in english
how to pronounce comment
comments synonym
comment meaning in french
comment verb
comment or comments
comments meaning in tamil
define remark

This is the rspec binstub in my project directory.

#!/usr/bin/env ruby
begin
  load File.expand_path("../spring", __FILE__)
rescue LoadError
end
# frozen_string_literal: true
#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
  Pathname.new(__FILE__).realpath)

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rspec-core", "rspec")

What is this intended to do?

# frozen_string_literal: true

# frozen_string_literal: true is a magic comment, supported for the first time in Ruby 2.3, that tells Ruby that all string literals in the file are implicitly frozen, as if #freeze had been called on each of them. That is, if a string literal is defined in a file with this comment, and you call a method on that string which modifies it, such as <<, you'll get RuntimeError: can't modify frozen String.

The comment must be on the first line of the file.

In Ruby 2.3, you can use this magic comment to prepare for frozen string literals being the default in Ruby 3.

In Ruby 2.3 run with the --enable=frozen-string-literal flag, and in Ruby 3, string literals are frozen in all files. You can override the global setting with # frozen_string_literal: false.

If you want a string literal to be mutable regardless of the global or per-file setting, you can prefix it with the unary + operator (being careful with operator precedence) or call .dup on it:

# frozen_string_literal: true
"".frozen?
=> true
(+"").frozen?
=> false
"".dup.frozen?
=> false

You can also freeze a mutable (unfrozen) string with unary -.

Comment, gossip; talk: His frequent absences gave rise to comment. a criticism or interpretation, often by implication or suggestion: The play is a comment on modern  a criticism or interpretation, often by implication or suggestion: The play is a comment on modern society.

Comment definition and meaning, comment meaning: 1. something that you say or write that expresses your opinion: 2. used to say that you do not…. Learn more. In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.

In Ruby 3.0. Matz (Ruby’s creator) decided to make all String literals frozen by default.

You can use in Ruby 2.x. Just add this comment in the first line of your files.

# frozen_string_literal: true

The above comment at top of a file changes semantics of static string literals in the file. The static string literals will be frozen and always returns same object. (The semantics of dynamic string literals is not changed.)

This way has following benefits:

No ugly f-suffix. No syntax error on older Ruby. We need only a line for each file.

Plese, read this topic for more information.

https://bugs.ruby-lang.org/issues/8976

Comment, comment meaning, definition, what is comment: an opinion that you express about someon: Learn more. Highlight the text you want to comment on. Open the Review ribbon and select New Comment. Type your comment in the field that appears in the right margin. It contains your name and a time stamp that's visible to others. If you need to edit your comment, just click on the comment box and make the change.

COMMENT, What does comment mean? comment is defined by the lexicographers at Oxford Dictionaries as A verbal or written remark expressing an opinion or reaction. A comment is a note or annotation that an author or reviewer can add to a document. Microsoft Word displays the comment in the Reviewing pane or in a balloon in the margin of the document. This article answers some frequently asked questions about the Comment feature in Word. A balloon shows markup elements (for example, comments) in the

comment, Definition of comment in the AudioEnglish.org Dictionary. Meaning of comment. What does comment mean? Proper usage and audio pronunciation (plus IPA  Highlighted Comments is a "tag" that you see when you choose to view or reply to a comment from a YouTube comment notification. In most cases it could be from the new activity YouTube notification

Comment, The highlighted comment feature on YouTube helps you navigate the many comments on YouTube videos. Here's what a highlighted comment  To edit a particular comment in a thread, tap the cell (that has the comment), tap the purple comment icon that appears above it, tap the particular comment you want to edit, and then tap Edit. Modify the comment as necessary and then click Save .

Comments
  • Important thing to notice regarding freezing strings is that it improves app's performance. See also here
  • @dave-schweisguth Shouldn't we expect -"foo" to be the same as "foo".freeze? When I check (-"foo").__id__ I get a different value each time, but "foo".freeze.__id__ is same each time. Any ideas?
  • I wonder if this function is the issue, it seems to only be called with the unary minus. github.com/ruby/ruby/blob/trunk/string.c#L2572
  • - is for deduplicating the String to save memory, in addition to returning a frozen String.
  • While you still can use the magic comment, Matz officially decided not to make all string literals immutable by default in Ruby 3: bugs.ruby-lang.org/issues/11473#note-53
  • Upvote for The Man In Black (and the answer).
  • Unfortunately this comment does not work for strings in arrays, so they still need to be freezed explicitly
  • Unfortunately this will not be in ruby 3 bugs.ruby-lang.org/issues/11473#note-53