在ruby脚本中实现dry-run

时间:2016-04-20 11:30:09

标签: ruby bash dry

有人知道如何在Ruby中实现干运行选项吗?

我需要这样的东西,但仅限于红宝石 https://serverfault.com/questions/147628/implementing-dry-run-in-bash-scripts

我试过这个,但是其他部分不起作用:

DRY_RUN = true

def perform(*args)
  command = args
  if DRY_RUN
    command.each{|x| puts x}
  else
   command.each {|x| x}
  end
end

perform("puts 'Hello'")

提前感谢任何想法。

P.S我不想使用类似系统("ruby -e \"puts 'Hello'\"")

的东西

1 个答案:

答案 0 :(得分:1)

这可能有所帮助:

def perform(*commands)
  commands.each { |x| DRY_RUN ? puts(x) : eval(x)}
end

结果是:

DRY_RUN = true
perform("puts 'Hello'")
  

put' Hello'

     

=> [" put'你好'"]

DRY_RUN = false
perform("puts 'Hello'")
  

您好

     

=> [" put'你好'"]