1

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

0

1 Answer 1

0

On the else sentence, where you have:

command.each { |x| x }

Replace that with either system(x) if you are running a system command, or eval(x) if you are trying to run ruby code, like:

DRY_RUN = true

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

or

DRY_RUN = true

def perform(*args)
  command = args
  if DRY_RUN
    command.each{ |x| puts x }
  else
   command.each { |x| eval(x) }
  end
end
1
  • Yes! It works pretty good! Thank you a lot, @rorra. Problem resolved.
    – jumpy
    Apr 20, 2016 at 11:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.