2012-10-02 56 views
0

在我的capistrano任務中,我打電話給superfunction方法。不幸的是,它拋出了Unexpected Return錯誤。我需要從superfunction方法中獲取輸出,以便在我的任務中進一步解析它。從Ruby方法返回變量/數組時出錯

def superfunction(cmd_type, command, client) 
    run "#{command}" do |channel, stream, data| 
     hostname = "#{channel[:host]}".tr('"','') 
     result = "#{data}".to_s.strip 
     return hostname, result 
    end 

end 


task :gather, :roles => :hosts do 
... 
    servername, redhat_version = superfunction("redhat_version", "cat /etc/redhat-release", client) 
end 

回答

0

錯誤正在產生,因爲您的方塊已被返回後(可能capistrano在內部存儲它)調用。作爲一個簡單的解決方法,您可以通過使用塊獲取想要的變量:

def superfunction(cmd_type, command, client) 
    run "#{command}" do |channel, stream, data| 
    hostname = "#{channel[:host]}".tr('"','') 
    result = "#{data}".to_s.strip 
    yield(hostname, result) 
    end 
end 

task :gather, :roles => :hosts do 
    superfunction("redhat_version", "cat /etc/redhat-release", client) do |servername, redhat_version| 
    # Use servername and redhat_version here 
    end 
end