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

  • SEARCH
  • Home
  • 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 6206911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:32:08+00:00 2026-05-24T05:32:08+00:00

Overview I am creating objects in my ruby script from database queries that generates

  • 0

Overview

I am creating objects in my ruby script from database queries that generates XML files. I have made it so only one XML file is processed at a time and all of the tags are generic so other queries can be added easily.

Problem

I am creating one object at a time, then adding it to a list, like so:

#create a new BarChart
bar_chart = BarChart.new(title, data, labels, x_axis, y_axis);

#add the chart to the chart list
charts.push(bar_chart)

But every time I process an XML file a want to create a BarChart I am reusing the variable bar_chart which is causing the data of my objects to be overwritten. I am looking for a way around this.

What I’ve tried

I have tried to pass a copy of the object into the list, but that is still overwriting the data.

#create a new BarChart
bar_chart = BarChart.new(title, data, labels, x_axis, y_axis);

#add the chart to the chart list
charts.push(bar_chart.clone)

and

#create a new BarChart
bar_chart = BarChart.new(title, data, labels, x_axis, y_axis);

#add the chart to the chart list
charts.push(bar_chart.dup)

Any help/ideas would be great.
Thanks.

EDIT, more information
Here is the method I do the XML processing in.

def self.process_xml_files2(filenames)
    labels = []
    data  = []
    charts = []
    title    = nil
    type   = nil
    x_axis  = nil
    y_axis  = nil

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

            #search for tags and append correct data to lists
            if tag.casecmp('Type') == 0
            type = text
        elsif tag.casecmp('Title') == 0
                title = text
            elsif tag.casecmp('Labels') == 0
                labels.push(text)
            elsif tag.casecmp('Data') == 0
                data.push(text)
            elsif tag.casecmp('X-Axis') == 0
                x_axis = text
            elsif tag.casecmp('Y-Axis') == 0
                y_axis = text
            end
        end
        f.close()

        #test for correct chart parameters
        raise "Not Enough Arguments" 
            if title == nil or type == nil or data.empty? or labels.empty?

        #process the raw chart data 
        if type.casecmp('Bar') == 0
            #test for labels
            raise "Bar Charts require X and Y axis labels" 
                    if x_axis == nil or y_axis == nil

            #format the data for the bar chart
            data = BarChart.barify_data(data)

            #create a new BarChart
            bar_chart = BarChart.new(title, data, labels, x_axis, y_axis);

            #add the chart to the chart list
            charts.push(bar_chart)
        elsif type.casecmp('Pie') == 0
            #format data and labels for the pie chart
            data = PieChart.pieify_data(data)

            #create a new Pie Chart
            pie_chart = PieChart.new(title, data, labels)

            #add the pie chart to the chart list
            charts.push(pie_chart.clone)
            else
            raise "Invalid Chart Type: Not Pie or Bar"
        end
    end

        #write all the charts to the images directory
    charts.each do |ch|
        puts ch.url + "\n\n"
        ch.download_image(ch.url, ch.title)
    end
end
  • 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-24T05:32:09+00:00Added an answer on May 24, 2026 at 5:32 am

    From what I can see in the code, you are reusing labels and data objects (be careful: objects, not variables!) for every chart you append to the list. It seems that you should move

    labels = []
    data = []
    

    initializations inside the filenames.each loop.

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

Sidebar

Related Questions

Overview So I have pulled out a document from my database. Inside is a
Overview: I have an array of 20 byte strings that needs to be stored
I have a overview page containing a list with some links from which multiple
I want to get an overview of files that are updated in TFS (that
Overview: I have an application that sometimes must make something with celery- and if
Is there any way to see an overview of what kind of queries are
I'm creating a JavaScript-driven timetable overview screen. It shows times of day as rows
I am creating a plugin and it cannot access $(this). A simple overview of
Overview: I'm designing a restaurant management application, and I have an entity called Order
I'm hoping for some insight from someone better experienced than I for creating a

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.