jumpy
jumpy

Reputation: 337

Implementing dry-run in ruby script

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

Upvotes: 2

Views: 555

Answers (1)

Maxim Pontyushenko
Maxim Pontyushenko

Reputation: 3053

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

Upvotes: 1

Related Questions