2016-04-20 6 views
1

kann jemand Trockenlauf-Option in Ruby zu implementieren?Implementierung von Trockenlauf in Ruby

Ich brauche so etwas, aber nur für Rubin. https://serverfault.com/questions/147628/implementing-dry-run-in-bash-scripts

Ich habe dies versucht, aber Teil nach else funktioniert nicht:

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

Vielen Dank für jede Idee im Voraus.

PS Ich will nicht Gebrauch etwas wie system("ruby -e \"puts 'Hello'\"")

Antwort

0

Auf dem anderen Satz, in dem Sie haben:

command.each { |x| x } 

dass Ersetzen entweder mit system(x), wenn Sie einen Systembefehl ausgeführt wird, oder wenn eval(x) Sie versuchen, ruby-Code, wie zu laufen:

DRY_RUN = true 

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

oder

DRY_RUN = true 

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

Ja! Es funktioniert ziemlich gut! Vielen Dank, @rorra. Problem gelöst. – jumpy