简体   繁体   English

Ruby Rubocop。 MutableConstant-未检测到冻结

[英]Ruby Rubocop. MutableConstant - not detecting freeze

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. 我编写了一个lib / animal.rb,其中包含多个参数列表,我想在控制器中引用该列表并将其添加到我的参数列表中。 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 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. 从功能上来说,它工作得很好,但是我看到一个奇怪的rubocop错误。 I would prefer to not disable MutableConstant for this section (disabling rubocop is usually a band aid that you pay for at some point). 我宁愿不禁用此部分的MutableConstant(禁用rubocop通常是您需要付费的创可贴)。

Rubocop error Rubocop错误

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. 我调查了这个问题: Ruby rubocop:如何冻结由splat生成的数组常量但是我的数组已经是数组,所以我觉得它不适用于我/不必调用to_a。

As @drenmi suggested, it was an older version of rubocop giving me this error. 正如@drenmi所建议的,这是rubocop的较旧版本,给了我这个错误。 Once I upgraded to 0.46.0 the error was no longer. 一旦我升级到0.46.0,该错误就不再存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM