Ruby: magic comments “frozen_string_literal: true” vs “immutable: string”
up vote
3
down vote
favorite
In ruby one can freeze all constant strings in a file via two different magic comments at the beginning of a file:
# frozen_string_literal: true
and
# -*- immutable: string -*-
I have no idea what the differences are.
Are there any?
ruby
add a comment |
up vote
3
down vote
favorite
In ruby one can freeze all constant strings in a file via two different magic comments at the beginning of a file:
# frozen_string_literal: true
and
# -*- immutable: string -*-
I have no idea what the differences are.
Are there any?
ruby
possible duplicate of stackoverflow.com/questions/37799296/…
– lacostenycoder
Nov 12 at 19:53
1
The difference is that the first one works and the second one has nothing to do with Ruby and thus doesn't do anything.#
– Jörg W Mittag
Nov 12 at 21:09
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
In ruby one can freeze all constant strings in a file via two different magic comments at the beginning of a file:
# frozen_string_literal: true
and
# -*- immutable: string -*-
I have no idea what the differences are.
Are there any?
ruby
In ruby one can freeze all constant strings in a file via two different magic comments at the beginning of a file:
# frozen_string_literal: true
and
# -*- immutable: string -*-
I have no idea what the differences are.
Are there any?
ruby
ruby
asked Nov 12 at 17:55
dCSeven
527
527
possible duplicate of stackoverflow.com/questions/37799296/…
– lacostenycoder
Nov 12 at 19:53
1
The difference is that the first one works and the second one has nothing to do with Ruby and thus doesn't do anything.#
– Jörg W Mittag
Nov 12 at 21:09
add a comment |
possible duplicate of stackoverflow.com/questions/37799296/…
– lacostenycoder
Nov 12 at 19:53
1
The difference is that the first one works and the second one has nothing to do with Ruby and thus doesn't do anything.#
– Jörg W Mittag
Nov 12 at 21:09
possible duplicate of stackoverflow.com/questions/37799296/…
– lacostenycoder
Nov 12 at 19:53
possible duplicate of stackoverflow.com/questions/37799296/…
– lacostenycoder
Nov 12 at 19:53
1
1
The difference is that the first one works and the second one has nothing to do with Ruby and thus doesn't do anything.#
– Jörg W Mittag
Nov 12 at 21:09
The difference is that the first one works and the second one has nothing to do with Ruby and thus doesn't do anything.#
– Jörg W Mittag
Nov 12 at 21:09
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
The 1st syntax is the magic comment for Ruby 2.3+ versions to freeze string literals, otherwise you have to use the String method like this:
'hello world!'.freeze
The 2nd syntax is not implemented in Ruby, however it is the way that variables are specified for files in the Emacs text editor.
For example, the following comment in Emacs would declare that the file is a Ruby file and needs Ruby syntax highlighting, and that the variable immutable is set to the value string.
# -*- mode: ruby; immutable: string -*-
After searching around, it looks like that does nothing and is not used by any Ruby syntax highlighting mode.
So you do not need the 2nd syntax.
are you sure 1st version works in Ruby < 2.3 ?? , I tested 2.1.2 and it does not.
– lacostenycoder
Nov 12 at 21:16
1
@lacostenycoder apparently the feature was targeted for 2.1 but ended up being in 2.2: bugs.ruby-lang.org/issues/9278 but it looks like it's 2.3+ only :/
– Rudolf Olah
Nov 12 at 21:52
add a comment |
up vote
3
down vote
Digging for anything on the 2nd version, it looks like they had the same intention but the 2nd magic comment syntax does not to appear to have been adopted as of Ruby 2.1.0.
See https://github.com/ruby/ruby/pull/487
The first version # frozen_string_literal: true was adopted in Ruby 2.3.0
I tried the latter version in a few versions of ruby but didn't work. I would guess it should not be used or trusted to work in any version of >= 2.3 but probably no versions support it. In fact, I was not able to find any reference to that version in the open source code on github searching that syntax
https://github.com/ruby/ruby/search?q=immutable%3A+string&unscoped_q=immutable%3A+string
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
The 1st syntax is the magic comment for Ruby 2.3+ versions to freeze string literals, otherwise you have to use the String method like this:
'hello world!'.freeze
The 2nd syntax is not implemented in Ruby, however it is the way that variables are specified for files in the Emacs text editor.
For example, the following comment in Emacs would declare that the file is a Ruby file and needs Ruby syntax highlighting, and that the variable immutable is set to the value string.
# -*- mode: ruby; immutable: string -*-
After searching around, it looks like that does nothing and is not used by any Ruby syntax highlighting mode.
So you do not need the 2nd syntax.
are you sure 1st version works in Ruby < 2.3 ?? , I tested 2.1.2 and it does not.
– lacostenycoder
Nov 12 at 21:16
1
@lacostenycoder apparently the feature was targeted for 2.1 but ended up being in 2.2: bugs.ruby-lang.org/issues/9278 but it looks like it's 2.3+ only :/
– Rudolf Olah
Nov 12 at 21:52
add a comment |
up vote
4
down vote
The 1st syntax is the magic comment for Ruby 2.3+ versions to freeze string literals, otherwise you have to use the String method like this:
'hello world!'.freeze
The 2nd syntax is not implemented in Ruby, however it is the way that variables are specified for files in the Emacs text editor.
For example, the following comment in Emacs would declare that the file is a Ruby file and needs Ruby syntax highlighting, and that the variable immutable is set to the value string.
# -*- mode: ruby; immutable: string -*-
After searching around, it looks like that does nothing and is not used by any Ruby syntax highlighting mode.
So you do not need the 2nd syntax.
are you sure 1st version works in Ruby < 2.3 ?? , I tested 2.1.2 and it does not.
– lacostenycoder
Nov 12 at 21:16
1
@lacostenycoder apparently the feature was targeted for 2.1 but ended up being in 2.2: bugs.ruby-lang.org/issues/9278 but it looks like it's 2.3+ only :/
– Rudolf Olah
Nov 12 at 21:52
add a comment |
up vote
4
down vote
up vote
4
down vote
The 1st syntax is the magic comment for Ruby 2.3+ versions to freeze string literals, otherwise you have to use the String method like this:
'hello world!'.freeze
The 2nd syntax is not implemented in Ruby, however it is the way that variables are specified for files in the Emacs text editor.
For example, the following comment in Emacs would declare that the file is a Ruby file and needs Ruby syntax highlighting, and that the variable immutable is set to the value string.
# -*- mode: ruby; immutable: string -*-
After searching around, it looks like that does nothing and is not used by any Ruby syntax highlighting mode.
So you do not need the 2nd syntax.
The 1st syntax is the magic comment for Ruby 2.3+ versions to freeze string literals, otherwise you have to use the String method like this:
'hello world!'.freeze
The 2nd syntax is not implemented in Ruby, however it is the way that variables are specified for files in the Emacs text editor.
For example, the following comment in Emacs would declare that the file is a Ruby file and needs Ruby syntax highlighting, and that the variable immutable is set to the value string.
# -*- mode: ruby; immutable: string -*-
After searching around, it looks like that does nothing and is not used by any Ruby syntax highlighting mode.
So you do not need the 2nd syntax.
edited Nov 12 at 21:54
answered Nov 12 at 20:32
Rudolf Olah
8,215115788
8,215115788
are you sure 1st version works in Ruby < 2.3 ?? , I tested 2.1.2 and it does not.
– lacostenycoder
Nov 12 at 21:16
1
@lacostenycoder apparently the feature was targeted for 2.1 but ended up being in 2.2: bugs.ruby-lang.org/issues/9278 but it looks like it's 2.3+ only :/
– Rudolf Olah
Nov 12 at 21:52
add a comment |
are you sure 1st version works in Ruby < 2.3 ?? , I tested 2.1.2 and it does not.
– lacostenycoder
Nov 12 at 21:16
1
@lacostenycoder apparently the feature was targeted for 2.1 but ended up being in 2.2: bugs.ruby-lang.org/issues/9278 but it looks like it's 2.3+ only :/
– Rudolf Olah
Nov 12 at 21:52
are you sure 1st version works in Ruby < 2.3 ?? , I tested 2.1.2 and it does not.
– lacostenycoder
Nov 12 at 21:16
are you sure 1st version works in Ruby < 2.3 ?? , I tested 2.1.2 and it does not.
– lacostenycoder
Nov 12 at 21:16
1
1
@lacostenycoder apparently the feature was targeted for 2.1 but ended up being in 2.2: bugs.ruby-lang.org/issues/9278 but it looks like it's 2.3+ only :/
– Rudolf Olah
Nov 12 at 21:52
@lacostenycoder apparently the feature was targeted for 2.1 but ended up being in 2.2: bugs.ruby-lang.org/issues/9278 but it looks like it's 2.3+ only :/
– Rudolf Olah
Nov 12 at 21:52
add a comment |
up vote
3
down vote
Digging for anything on the 2nd version, it looks like they had the same intention but the 2nd magic comment syntax does not to appear to have been adopted as of Ruby 2.1.0.
See https://github.com/ruby/ruby/pull/487
The first version # frozen_string_literal: true was adopted in Ruby 2.3.0
I tried the latter version in a few versions of ruby but didn't work. I would guess it should not be used or trusted to work in any version of >= 2.3 but probably no versions support it. In fact, I was not able to find any reference to that version in the open source code on github searching that syntax
https://github.com/ruby/ruby/search?q=immutable%3A+string&unscoped_q=immutable%3A+string
add a comment |
up vote
3
down vote
Digging for anything on the 2nd version, it looks like they had the same intention but the 2nd magic comment syntax does not to appear to have been adopted as of Ruby 2.1.0.
See https://github.com/ruby/ruby/pull/487
The first version # frozen_string_literal: true was adopted in Ruby 2.3.0
I tried the latter version in a few versions of ruby but didn't work. I would guess it should not be used or trusted to work in any version of >= 2.3 but probably no versions support it. In fact, I was not able to find any reference to that version in the open source code on github searching that syntax
https://github.com/ruby/ruby/search?q=immutable%3A+string&unscoped_q=immutable%3A+string
add a comment |
up vote
3
down vote
up vote
3
down vote
Digging for anything on the 2nd version, it looks like they had the same intention but the 2nd magic comment syntax does not to appear to have been adopted as of Ruby 2.1.0.
See https://github.com/ruby/ruby/pull/487
The first version # frozen_string_literal: true was adopted in Ruby 2.3.0
I tried the latter version in a few versions of ruby but didn't work. I would guess it should not be used or trusted to work in any version of >= 2.3 but probably no versions support it. In fact, I was not able to find any reference to that version in the open source code on github searching that syntax
https://github.com/ruby/ruby/search?q=immutable%3A+string&unscoped_q=immutable%3A+string
Digging for anything on the 2nd version, it looks like they had the same intention but the 2nd magic comment syntax does not to appear to have been adopted as of Ruby 2.1.0.
See https://github.com/ruby/ruby/pull/487
The first version # frozen_string_literal: true was adopted in Ruby 2.3.0
I tried the latter version in a few versions of ruby but didn't work. I would guess it should not be used or trusted to work in any version of >= 2.3 but probably no versions support it. In fact, I was not able to find any reference to that version in the open source code on github searching that syntax
https://github.com/ruby/ruby/search?q=immutable%3A+string&unscoped_q=immutable%3A+string
edited Nov 12 at 20:28
answered Nov 12 at 20:23
lacostenycoder
3,50011226
3,50011226
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267596%2fruby-magic-comments-frozen-string-literal-true-vs-immutable-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
possible duplicate of stackoverflow.com/questions/37799296/…
– lacostenycoder
Nov 12 at 19:53
1
The difference is that the first one works and the second one has nothing to do with Ruby and thus doesn't do anything.#
– Jörg W Mittag
Nov 12 at 21:09