2012-12-07 110 views
2

編輯:問題是無法獲取散列內數組的數量,因此它可以是x =數組量。所以它可以用作function.each_index {| x |代碼}無法將符號轉換爲散列表中的整數

嘗試使用行的量的指數爲依賴於有多少數據從CSV文件拉出重複次數的動作X量的一種方式。

終端發出

=> Can't convert symbol to integer (TypeError) 

完全錯誤:

=> ~/home/tests/Product.rb:30:in '[]' can't convert symbol into integer (TypeError) from ~home/tests/Product.rub:30:in 'getNumbRel' 
from test.rb:36:in '<main>' 

功能是執行的操作是:

def getNumRel 
    if defined? @releaseHashTable 
    return @releaseHashTable[:releasename].length 
    else 
     @releaseHashTable = readReleaseCSV() 
     return @releaseHashTable[:releasename].length 
    end 
end 

CSV數據拉只是一個數組的哈希,沒什麼時髦的。

def readReleaseCSV() 
    $log.info("Method "+"#{self.class.name}"+"."+"#{__method__}"+" has started") 
    $log.debug("reading product csv file") 
    # Create a Hash where the default is an empty Array 
    result = Array.new 
    csvPath = "#{File.dirname(__FILE__)}"+"/../../data/addingProdRelProjIterTestSuite/releaseCSVdata.csv" 
    CSV.foreach(csvPath, :headers => true, :header_converters => :symbol) do |row| 
    row.each do |column, value| 
     if "#{column}" == "prodid" 
     proHash = Hash.new { |h, k| h[k] = [ ] } 
     proHash['relid'] << row[:relid] 
     proHash['releasename'] << row[:releasename] 
     proHash['inheritcomponents'] << row[:inheritcomponents] 

     productId = Integer(value) 
     if result[productId] == nil 
      result[productId] = Array.new 
     end 

     result[productId][result[productId].length] = proHash 
     end 
    end 
    end 
    $log.info("Method "+"#{self.class.name}"+"."+"#{__method__}"+" has finished") 
    @productReleaseArr = result 
end 
+0

你可以加回溯 – krichard

+0

@kritchard mhm也許是noob問題,但究竟是回溯? – samayres1992

+0

整個錯誤消息。 –

回答

2

對不起,無法抗拒,清理你的方法。

# empty brackets unnecessary, no uppercase in method names 
def read_release_csv 
    # you don't need + here 
    $log.info("Method #{self.class.name}.#{__method__} has started") 
    $log.debug("reading product csv file") 
    # you're returning this array. It is not a hash. [] is preferred over Array.new 
    result = [] 
    csvPath = "#{File.dirname(__FILE__)}/../../data/addingProdRelProjIterTestSuite/releaseCSVdata.csv" 
    CSV.foreach(csvPath, :headers => true, :header_converters => :symbol) do |row| 
    row.each do |column, value| 
     # to_s is preferred 
     if column.to_s == "prodid" 
     proHash = Hash.new { |h, k| h[k] = [ ] } 
     proHash['relid'] << row[:relid] 
     proHash['releasename'] << row[:releasename] 
     proHash['inheritcomponents'] << row[:inheritcomponents] 
     # to_i is preferred 
     productId = value.to_i 
     # this notation is preferred 
     result[productId] ||= [] 
     # this is identical to what you did and more readable 
     result[productId] << proHash 
     end 
    end 
    end 
    $log.info("Method #{self.class.name}.#{__method__} has finished") 
    @productReleaseArr = result 
end 
+0

哈哈謝謝,我一直都沒有腳本化ruby,我只知道基礎知識。這只是一個清理或者它是一個解決方案? – samayres1992

+0

嘿,它也燃燒了我的眼睛。考慮到其他代碼,我希望更進一步,因爲似乎有太多的數據結構,並且從一個轉換到另一個。它需要通過面向對象的方式進行OO操作,例如Product類等域對象。 –

2

你還沒有太多去,但現在看來,@releaseHashTable包含數組,而不是一個哈希值。

更新:根據您發佈的實現,您可以看到productId是一個整數,並且readReleaseCSV()的返回值是一個數組。

爲了得到你想要的releasename,你必須這樣做:

@releaseHashTable[productId][n][:releasename] 

其中productIdn是整數。要麼你必須具體指定它們,要麼(如果你不知道n),你必須引入一個循環來收集特定productId的所有產品的所有發行版名稱。

+0

嗯也許這是問題,這對我來說有點令人困惑,因爲我沒有編寫大部分腳本,我只是將更改應用於之前編寫的一些腳本。也許我對它是如何工作的理解是我出錯的地方。 – samayres1992

1

這是馬克托馬斯的意思是:

> a = [1,2,3] # => [1, 2, 3] 
> a[:sym] 
TypeError: can't convert Symbol into Integer 
# here starts the backstrace 
    from (irb):2:in `[]' 
    from (irb):2 

數組是隻有像這樣a[1]此獲取從陣列
你回報的陣列而這就是爲什麼你的代碼沒有第二個元素的索引訪問:

#.... 
result = Array.new 
#.... 
@productReleaseArr = result 
# and then later on you call 
@releaseHashTable = readReleaseCSV() 
@releaseHashTable[:releasename] # which gives you TypeError: can't convert Symbol into Integer 
+0

啊我看到誤解了,函數取了應該提供數組數的row.length。這是我在測試中需要使用的。 – samayres1992

相關問題