开发者

Ruby object clone/copy

Overview

I am crea开发者_如何学编程ting 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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜