在ruby脚本中实现dry-run

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

我需要这样的东西,但仅限于rubyhttps://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'") 

提前感谢任何想法。

PS我不想使用像system这样的东西("ruby -e \"puts 'Hello'\"")

这有助于:

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

它导致:

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

放’你好’

=> [“把’你好’”]

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

你好

=> [“把’你好’”]