TyMac TyMac - 21 days ago 6
Ruby Question

Should this chef recipe be refactored?

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

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.

Is there a better way to do this?

Answer

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

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.

Comments