Ruby Rubocop. MutableConstant - not detecting freeze

361 views Asked by At

I wrote a lib/animal.rb with several lists of params, and I want to reference that list in my controller and add it my params list. I did this because I use this list in several locations and didn't want to litter my code with a bunch of references to the library.

Controller

ANIMAL_TYPE_INPUT_PARAMS = *Animals::ANIMAL_TYPE_PARAMS.freeze

....

def familar_params
  params.permit(ANIMAL_TYPE_INPUT_PARAMS, OTHER_PARAM_LIST....)
end

Lib/animal.rb

module Animal

  # param lists
  ANIMAL_TYPE_PARAMS = [
    :animal_has_fur, :animal_id, :animal_weight
  ].freeze
end

Functionally it works just fine, but I am seeing a weird rubocop error. I would prefer to not disable MutableConstant for this section (disabling rubocop is usually a band aid that you pay for at some point).

Rubocop error

app/controllers/api/v1/example_controller.rb:55:24: C: Freeze mutable objects assigned to constants.
  ANIMAL_TYPE_INPUT_PARAMS = *Animals::ANIMAL_TYPE_PARAMS.freeze
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I looked into this question: Ruby rubocop: how to freeze an array constant generated with splat But mine are already arrays, so I feel like it doesn't apply to me / shouldn't have to call to_a.

1

There are 1 answers

0
alex_milhouse On BEST ANSWER

As @drenmi suggested, it was an older version of rubocop giving me this error. Once I upgraded to 0.46.0 the error was no longer.