2011-08-01 31 views
1

概述Ruby對象克隆/複製在我的Ruby腳本

我創建的對象。我已經做到了,所以一次只能處理一個XML文件,而且所有標記都是通用的,因此可以輕鬆添加其他查詢。

問題

我在一次創建一個對象,然後將其添加到列表中,像這樣:

#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) 

但每次我處理XML時提交想創建一個BarChart我正在重複使用導致我的對象的數據被覆蓋的變量bar_chart。我正在尋找解決方法。

我已經試過

我試圖對象的副本傳遞到列表中,但仍然覆蓋數據。

#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) 

#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) 

任何幫助/意見將是巨大的。 謝謝。

編輯,更多信息 這裏是我做的XML處理方法

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 
+0

什麼是圖表類型?你自己實施了BarChart嗎? – Matt

+0

是BarChart是我創建的自定義類型。 –

+0

當您執行'bar_chart = BarChart.new ...'時,您以前創建的對象絕對不會被覆蓋。你在其他地方有問題,也許重新使用XML對象,我認爲你以某種方式包裝你的BarChart對象? –

回答

2

從我可以在代碼中看到,正在重用labelsdata對象(注意:對象,而不是變量!)爲您追加到列表的每個圖表。看來,你應該將

labels = [] 
data = [] 

初始化filenames.each循環。

+0

謝謝。我多次瀏覽了代碼,仍然沒有發現。 –