Nabil Kadimi
Nabil Kadimi

Reputation: 10424

Ruby: How can I group multiple blocks?

I have this code where execute is a Chef resource:

execute 'phpbrew_0' do
    command 'phpbrew --verbose install 5.6 +apxs2'
end

execute 'phpbrew_1' do
    command 'phpbrew --verbose install 7.0 +apxs2'
end

execute 'phpbrew_2' do
    command 'phpbrew --verbose install 7.1 +apxs2'
end

Does ruby have this feature where I can provide a collection of command and have them all be passed to the execute resource. Something like Javascript's Array.map?

Upvotes: 1

Views: 44

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230561

Yep, Hash#each, for example: https://ruby-doc.org/core-2.4.1/Hash.html#method-i-each

resource_to_command = {
  'phpbrew_14' => 'phpbrew --verbose install 5.6 +apxs2',
  'foo' => 'bar'
}

resource_to_command.each do |resource, cmd_text|
  execute resource do
    command cmd_text
  end
end

Upvotes: 2

Related Questions