动态常量分配错误

时间:2011-07-19 15:07:10

标签: ruby

我正在执行一个查询并试图将它存储在一个正常的工作中,直到我今天对源文件进行了更改,但这让我感到困惑,因为我没有更改查询或更改任何交互的代码有了这个查询。

以下是代码:

require 'rexml/document'
include REXML

def parallel_sort(data, labels)
    #implement a basic ruby styled selection sort in to sort the data in descending order
    raise "unequal arrays" if  data.length != labels.length

    0.upto(data.length - 2) do |i|
        max = i # largest value
        (i+1).upto(data.length - 1) { |j| max = j if data[j] > data[max] } #try to find new max
        data[i], data[max], labels[i], labels[max] = data[max], data[i], labels[max], labels[i] if i != max #swap values in both arrays
    end

    return (data, labels)
end

def process_xml_files(filenames)
    builds = ["Total Builds", "Failed Builds", "Successful Builds"]
    outFile = File.new("gchart-data.txt", "w")
    #outFile = File.new("parallel_sort_test.txt", "w")
    yearly_data    = []
    yearly_labels  = []
    monthly_data   = []
    monthly_labels = []
    failure_data   = []
    failure_labels = []
    failure_percents = []
    hash = []

    #retrieve needed data from the XML file
    filenames.each do |filename|
        #create a document
         doc = Document.new(File.new(filename))
         doc.elements.each("//row/field") do |e|
         name = e.attributes['name']
         text = e.text

         #search for tags and append correct data to lists
         if builds.include?(name)
            hash.push(name)
            hash.push(text)
         else
            if name == "Month-Year"
                yearly_labels.push(text)
            elsif name == "Past-Year-Builds"
                yearly_data.push(text)
            elsif name == "Month-Day"
                monthly_labels.push(text)
            elsif name == "Past-Month-Builds"
                monthly_data.push(text)
            elsif name == "Failure Type"
                failure_labels.push(text)
            else 
                temp = 100*(text.to_f() / FAILED_BUILDS.to_f())
                failure_data.push(temp)
                failure_percents.push(((100*temp).round/100.0).to_s() + "%")
            end
        end
        end
    end 

    #sort the failure_percents and failure_labels arrays together
    failure_percents, failure_labels = parallel_sort(failure_percents, failure_labels)

    outFile.puts hash
    outFile.puts "\nYearly Data\n#{yearly_data.join(",")}"
    outFile.puts yearly_labels
    outFile.puts "\nMonthly Data\n#{monthly_data.join(",")}"
    outFile.puts monthly_labels
    outFile.puts "\nFailure Data\n#{failure_data.join(",")}"
    outFile.puts failure_labels.zip(failure_percents).join("|") 
end

def execute_queries()
    FAILED_BUILDS = `mysql -h hostname -u root -D database -e 
    "SELECT COUNT(id) FROM builds WHERE buildstatus = 3 
    AND DATE(submittime) >= 
    DATE_SUB(CURDATE(), INTERVAL 1 YEAR);"`.gsub!(/[A-Za-z()\s]+/,"")

    #get the total number of builds
    totalBuilds = `mysql -h hostname -u root -D database  -X -e 
    "SELECT COUNT(buildid) 'Total Builds' FROM builds 
    WHERE DATE(submittime) >= 
    DATE_SUB(CURDATE(), INTERVAL 1 YEAR);" > total-builds.xml`

    #get the number of successful builds
    successBuilds = `mysql -h hostname -u root -D database -X -e 
    "SELECT COUNT(buildid) 'Successful Builds' FROM builds 
    WHERE buildstatus = 2 AND DATE(submittime) >= 
    DATE_SUB(CURDATE(), INTERVAL 1 YEAR);" > success-builds.xml`

    #get the number of failed builds 
    failedBuilds = `mysql -h hostname -u root -D database -X -e 
    "SELECT COUNT(buildid)'Failed Builds' FROM builds 
    WHERE buildstatus = 3 AND DATE(submittime) >= 
    DATE_SUB(CURDATE(), INTERVAL 1 YEAR);" > failed-builds.xml`

    #get the number of builds per month
    months = `mysql -h hostname -u root -D database -X -e 
    "select count(id) as 'Past-Year-Builds', 
    CONCAT(SUBSTR(MONTHNAME(submittime), 1,3), '-',
    YEAR(submittime)) as 'Month-Year' from builds where 
    DATE(submittime) >= DATE_SUB(CURDATE(), interval 1 year)
    group by YEAR(submittime), MONTH(submittime);" > year-builds.xml`

    days = `mysql -h hostname -u root -D database -X -e 
    "SELECT COUNT(id) AS 'Past-Month-Builds', 
    CONCAT(SUBSTR(MONTHNAME(submittime), 1, 3), '-', 
    DAY(submittime)) as 'Month-Day' FROM builds
    WHERE DATE(submittime) >= DATE_SUB(CURDATE(), INTERVAL 30 day) 
    GROUP BY MONTH(submittime), DAY(submittime);" > month-builds.xml`

    failures = `mysql -h hostname -u root -D database -X -e 
    "select F.name 'Failure Type', count(B.id) 'Failure Count' 
    from builds B JOIN failureareas F ON B.failurearea = F.id 
    where Date(B.submittime) >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR) 
    AND B.buildstatus != 2 GROUP BY F.name;" > failures.xml`
end


def start()
    #grab data from the database
    execute_queries()

    #collect the xml files from the current directory
    filenames = Dir.glob("*.xml")

    #merge the xml files together
    finalXML = process_xml_files( filenames )

    #clean up directory 
    filenames.each do |filename|
        del = `rm #{filename}`
    end
end

#test for execution
if __FILE__ == $0
    start()
end

所以有人能告诉我为什么这不起作用?我只分配给常数一次。

这是错误:

gchart-url.rb:14: syntax error
gchart-url.rb:75: dynamic constant assignment
    FAILED_BUILDS = `mysql -h hostname -u root -D database -e 
    "SELECT COUNT(id) FROM builds WHERE buildstatus = 3 
    AND DATE(submittime) >= 
    DATE_SUB(CURDATE(), INTERVAL 1 YEAR);"`.gsub!(/[A-Za-z()\s]+/,"")
                   ^
gchart-url.rb:114: syntax error

任何帮助都会很棒。 感谢。

编辑:很抱歉这篇长篇文章

1 个答案:

答案 0 :(得分:2)

首先,你应该从第一个报告错误中解决语法错误 - 通常其余的都会消失。

在第14行中,更改return以返回数组:

  return [data, labels]
end

Ruby认为你不应该从方法中设置常量。从类定义(在任何'def'块之外)设置它不会是一个错误。如果您真的想从方法创建常量,请使用:

self.class.const_set(:FAILED_BUILDS, `mysql -h ....

但我的问题是:你为什么要设定常数?实例变量可能是更好的解决方案。