Implementing dry-run in ruby script

时光总嘲笑我的痴心妄想 提交于 2020-01-11 10:32:11

问题


anybody know how to implement dry-run option in Ruby?

I need something like this, but only for ruby https://serverfault.com/questions/147628/implementing-dry-run-in-bash-scripts

I've tried this, but part after else doesn't work:

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'")

Thanks for any idea in advance.

P.S I don't want use something like system("ruby -e \"puts 'Hello'\"")


回答1:


This could help:

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

It results in:

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

puts 'Hello'

=> ["puts 'Hello'"]

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

Hello

=> ["puts 'Hello'"]



来源:https://stackoverflow.com/questions/36742456/implementing-dry-run-in-ruby-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!