无法将哈希表中的符号转换为整数

时间:2012-12-07 12:01:28

标签: ruby

编辑:问题是无法获得散列中的数组数量,因此它可以是,x =数组的数量。所以它可以用作function.each_index {| x |代码}

尝试使用行数索引作为重复操作X次的方式,具体取决于从CSV文件中提取的数据量。

终端发布

=> 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

3 个答案:

答案 0 :(得分:2)

你没有做太多的事情,但似乎@releaseHashTable包含一个数组,而不是一个哈希。

更新:根据您发布的实施,您可以看到productId是一个整数,readReleaseCSV()的返回值是一个数组。

为了得到你想要的releasename,你必须这样做:

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

其中productIdn是整数。要么你必须具体指定它们,要么(如果你不知道n),你必须引入一个循环来收集特定productId的所有产品的所有发行名。

答案 1 :(得分: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

答案 2 :(得分: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