Ruby rubocop: how to freeze an array constant generated by splat

I assign an array constant as follows:

NUMS = *(2..9)

Rubocop says

C: freeze mutable objects assigned to constants.
NUMS = * (2..9); ^^^^^


So I'm trying

NUMS = *(2..9).freeze

Rubocop says

C: Freeze mutable objects assigned to constants.
NUMS = * (2..9) .freeze; ^^^^^^^^^^^^^


Tried to

NUMS = (*(2..9)).freeze

Rubocop says

E: unexpected token tRPAREN (Using parsing Ruby 2.0, setting using the TargetRubyVersion parameter, in AllCops)
NUMS = (* (2..9)). freeze ^


Tried to

NUMS = [1, 2, 3, 4, 5, 6, 7, 8, 9].freeze

Rubocop says

== happy_robot_dance (no errors)

I say

My hand hurts from entering 1, 2, 3, ... 9

Is there a way to use splat to assign and freeze constants?

----------

Solutions

NUMS = (2..9).to_a.freeze

NUMS = Array(2..9).freeze
+4
3

RuboCop ( .)

issue , .

, , :

# rubocop:disable Style/MutableConstant
NUMS = *(2..9)
# rubocop:enable Style/MutableConstant

#to_a:

NUMS = (2..9).to_a.freeze
+5

, Rubocop - 2 , , . ?

why_do_i_exist = *(2..9)
NUMS = why_do_i_exist.freeze
0

:

NUMS = Array[*2..9].freeze

0

Source: https://habr.com/ru/post/1650103/


All Articles