Home>

rubocop -a will add freeze to the constant.
It doesn't matter in itself, but I'm having trouble with the following cases.

Media = {a:'Asahi', b:'every day', c:'Yomiuri'}
Media.defulat ='Other'


This is fixed as follows

Media = {a:'Asahi', b:'every day', c:'Yomiuri'} .freeze
Media.defulat ='Other'


Then an error will occur on the second line.

Is there a way to prevent an error from being frozen by rubocop?

  • Answer # 1

    If you want to create complex constants.tapThere is a hand to use (Rurima).

    Media = {a:'Asahi', b:'every day', c:'Yomiuri'} .tap do | hash |
      hash.default ='Other'
      hash.freeze
    end

  • Answer # 2

    Style/MutableConstantTodisable disableIsn't it good to do it?

    # rubocop: disable Style/MutableConstant
    MEDIA = {a:'Asahi', b:'every day', c:'Yomiuri'}
    # rubocop: enable Style/MutableConstant
    MEDIA.defulat ='Other'