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?










share|improve this question






















  • 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















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?










share|improve this question






















  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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.






share|improve this answer























  • 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




















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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer























    • 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

















    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.






    share|improve this answer























    • 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















    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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




















    • 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














    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






    share|improve this answer



























      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






      share|improve this answer

























        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






        share|improve this answer














        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







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 12 at 20:28

























        answered Nov 12 at 20:23









        lacostenycoder

        3,50011226




        3,50011226






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?