这个厨师食谱应该被重构吗?

时间:2021-10-05 00:23:31

I've written a recipe that installs Windows desktop apps from a databag:

我已经编写了一个从数据库安装Windows桌面应用程序的菜谱:

workstation_apps = data_bag_item('winapps','desktop_apps')

for package in workstation_apps['apps'] do
        chocolatey_package "#{package}" do
        action :install
        end
end

Running foodcritic I get an error:

运行食品评论家我得到一个错误:

FC002: Avoid string interpolation where not required: ./recipes/default.rb:23

As you can see, I added double quotes around #{package} so that it expands the variable and does not function as a comment.

如您所见,我在#{package}周围添加了双引号,以便它扩展变量,而不作为注释。

Is there a better way to do this?

有没有更好的办法?

2 个解决方案

#1


2  

Two things: first the use of for loops in Ruby is discouraged in favor of each loops. Second, the chocolatey package provider support multi-package operations so you can rewrite the recipe like this:

有两件事:首先,在Ruby中不鼓励使用for循环,而支持每个循环。其次,巧克力包装供应商支持多包操作,所以您可以像这样重写菜谱:

workstation_apps = data_bag_item('winapps','desktop_apps')
chocolatey_package workstation_apps['apps']

(remember that :install is the default action so you don't need to write it out)

(记住:安装是默认操作,所以不需要写出来)

#2


3  

You can replace "#{package}" to package.to_s. If package is a string object, simply package:

您可以将“#{package}”替换为package.to_s。如果包是字符串对象,只需包:

workstation_apps = data_bag_item('winapps','desktop_apps')

for package in workstation_apps['apps'] do
        chocolatey_package package do
                action :install
        end
end

Indented action ... line to make it clear that the line belong to a block.

缩进行动……行,以明确这一行属于一个块。

#1


2  

Two things: first the use of for loops in Ruby is discouraged in favor of each loops. Second, the chocolatey package provider support multi-package operations so you can rewrite the recipe like this:

有两件事:首先,在Ruby中不鼓励使用for循环,而支持每个循环。其次,巧克力包装供应商支持多包操作,所以您可以像这样重写菜谱:

workstation_apps = data_bag_item('winapps','desktop_apps')
chocolatey_package workstation_apps['apps']

(remember that :install is the default action so you don't need to write it out)

(记住:安装是默认操作,所以不需要写出来)

#2


3  

You can replace "#{package}" to package.to_s. If package is a string object, simply package:

您可以将“#{package}”替换为package.to_s。如果包是字符串对象,只需包:

workstation_apps = data_bag_item('winapps','desktop_apps')

for package in workstation_apps['apps'] do
        chocolatey_package package do
                action :install
        end
end

Indented action ... line to make it clear that the line belong to a block.

缩进行动……行,以明确这一行属于一个块。