gpt4 book ai didi

ruby - 平台系列与 rubocop 的阵列

转载 作者:行者123 更新时间:2023-12-05 05:17:24 30 4
gpt4 key购买 nike

我正在尝试在 Chef 中为 platform_family 使用一个数组,根据 Chef 文档,我有正确的语法,但 rubocop 正在踢我的屁股。

Chef 文档:https://docs.chef.io/dsl_recipe.html#value-for-platform-family

对 ruby 新手有什么想法吗?

 12 tcp_ports = value_for_platform_family(
13 ['debian', 'rhel'] => [22, 443, 4172, 60443],
14 'windows' => [443, 3389, 4172, 60443]
15 )

rubocop 错误是

recipes/recipe.rb:13:3: C: Style/WordArray: Use %w or %W for an array of words.
['debian', 'rhel'] => [22, 443, 4172, 60443],
^^^^^^^^^^^^^^^^^^

最佳答案

在处理集合时,Rubocop 添加:

Prefer %w to the literal array syntax when you need an array of words (non-empty strings without spaces and special characters in them). Apply this rule only to arrays with two or more elements.

# bad
STATES = ['draft', 'open', 'closed']

# good
STATES = %w[draft open closed]

请注意,从 Ruby 风格指南的角度来看这很糟糕,但代码仍然有效。

所以你的例子是:

%w[debian rhel]

关于ruby - 平台系列与 rubocop 的阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49289378/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com