Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6169487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:51:58+00:00 2026-05-23T22:51:58+00:00

I am executing a query and trying to store it in a constant which

  • 0

I am executing a query and trying to store it in a constant which was working fine until I made a change to the source file today, but it’s confusing to me because I didn’t change the query or alter any code that interacts with this query.

Here is the code:

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

So can anyone tell me why this doesn’t work? I am only assigning to the constant once.

Here is the error:

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

Any help would be great.
Thanks.

EDIT: Sorry for the long post

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:51:59+00:00Added an answer on May 23, 2026 at 10:51 pm

    First, you should sort out the syntax errors, starting from the first reported one – often the rest will disappear.

    In the line 14 change the return to return an array:

      return [data, labels]
    end
    

    Ruby thinks that you should not set a constant from a method. Setting it from a class definition (outside any ‘def’ block) would not be an error. If you really want to create a constant from a method, use this:

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

    But my question is: why do you want to set a constant? An instance variable may be a better solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to import a table in my database executing this query : CREATE
In Microsoft SQL Server 2008, when executing .query('{xpath}/text()') on an XML stream column value,
I'm executing a query like this select field from table; In that query, there
in DB2 while executing a query i got SqlException: DB2 SQL Error: SQLCODE=-668, SQLSTATE=57016
What I want to do is while executing MySQL query, passing column data into
while executing the following query, i get an error that there's an error in
I'm executing the following query: Select guiPolygonID, dbo.fn_Yval(p1X, p1Y, p2X, p2Y, 4.003318) From [vPolygonSegments]
I am successfully executing the following query in SQL Server 2008 built into VS2008:
I have encountered a strange behavior while executing an sql query on the Oracle
When executing the following (complete) SQL query on Microsoft SQL Server 2000: SELECT B.ARTIFACTTNS,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.