0

我希望有人可以帮助我。

我在红宝石中有这个方法:

def puppetrun_oneClass!
   ProxyAPI::Puppet.new({:url => puppet_proxy.url}).runSingle fqdn
end

然后我在这个其他方法中调用它:

def update_multiple_puppetrun_oneClass_deploy
   if @hosts.map(&:puppetrun_oneClass!).uniq == [true]
      notice "Successfully executed, check reports and/or log files for more details"
   else
      error "Some or all hosts execution failed, Please check log files for more information"
   end
end

其中@hosts是主机名数组。

现在,我想扩展 puppetrun_oneClass!接受@myDeploy参数,其中@myDeploy参数是一个包含字符串的变量。

我怎么能那样做??然后我应该如何调用修改后的方法?

谢谢!!

4

1 回答 1

0

您应该将其添加为参数,但这意味着您需要在map循环中声明一个长格式块。

新方法:

def puppetrun_oneClass!(deploy)
  # ... Code using `deploy` variable
end

新来电:

@hosts.map { |h| host.puppetrun_oneClass!(@myDeploy) }.uniq

Note that uniq is a pretty heavy handed approach here if you just want to see if any of them failed. You might want to try find which would stop at the first one that fails rather than blindly executing them all:

!@hosts.find { |h| !host.puppetrun_oneClass!(@myDeploy) }

This will ensure that none of them returned a false condition. If you want to run them all and look for errors, you might try:

 failures = @hosts.reject { |h| host.puppetrun_oneClass!(@myDeploy) }

 if (failures.empty?)
   # Worked
 else
   # Had problems, failures contains list of failed `@hosts`
 end

The first part returns an array of any @hosts entries that failed. It might be useful to capture this list and use it to produce a more robust error message, perhaps describing those that didn't work.

于 2013-05-13T16:37:06.417 回答