Ruby Can';t将符号从哈希表转换为整数
编辑:问题是无法获取散列中的数组数量,因此可以是,x=数组数量。因此它可以用作函数。每个|索引{| x |代码} 尝试使用行数索引作为重复操作X次数的一种方式,重复次数取决于从CSV文件中提取的数据量 码头发出Ruby Can';t将符号从哈希表转换为整数,ruby,Ruby,编辑:问题是无法获取散列中的数组数量,因此可以是,x=数组数量。因此它可以用作函数。每个|索引{| x |代码} 尝试使用行数索引作为重复操作X次数的一种方式,重复次数取决于从CSV文件中提取的数据量 码头发出 => Can't convert symbol to integer (TypeError) 完全错误: => ~/home/tests/Product.rb:30:in '[]' can't convert symbol into integer (TypeError)
=> 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>'
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
def readreleaseCV()
$log.info(“方法”+“{self.class.name}”+”+“{uuuuu方法”}”+”已启动)
$log.debug(“读取产品csv文件”)
#创建一个默认为空数组的哈希
结果=Array.new
csvPath=“#{File.dirname(_File_#)}”+“/../../data/addingprodreprojitertestSuite/releaseCSVdata.csv”
CSV.foreach(csvPath,:headers=>true,:header\u converters=>:symbol)do |行|
行。每个do |列,值|
如果“#{column}”==“prodid”
proHash=Hash.new{| h,k | h[k]=[]
proHash['relid']您没有给出太多的信息,但似乎@releaseHashTable
包含一个数组,而不是散列
Update:根据您发布的实现,您可以看到productId
是一个整数,readreleaseCV()
的返回值是一个数组
要获得所需的releasename
,必须执行以下操作:
@releaseHashTable[productId][n][:releasename]
其中productId
和n
是整数。您必须具体指定它们,或者(如果您不知道n
),您必须引入一个循环来收集特定productId
的所有产品的所有发布名称这就是Mark Thomas的意思:
> 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
一个数组只能通过像soa[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
抱歉,无法抗拒,清理了你的方法
# 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
#不需要空括号,方法名称中没有大写字母
def read_release_csv
#在这里你不需要+
$log.info(“方法{self.class.name}.{uuuuu方法}已启动”)
$log.debug(“读取产品csv文件”)
#您正在返回此数组。这不是一个杂烩。[]优先于Array.new
结果=[]
csvPath=“#{File.dirname(_File_#)}/./../data/addingprodreprojitertestSuite/releaseCSVdata.csv”
CSV.foreach(csvPath,:headers=>true,:header\u converters=>:symbol)do |行|
行。每个do |列,值|
#最好去美国
如果column.to_s==“prodid”
proHash=Hash.new{| h,k | h[k]=[]
proHash['relid']你能添加回溯吗please@kritchard嗯,这可能是一个noob问题,但回溯到底是什么?补充说,现在,希望它能帮助嗯,也许这就是问题所在,这对我来说有点困惑,因为我没有编写大多数脚本,我只是将更改应用于以前编写的一些脚本。也许我对它如何工作的理解是我出错的地方。啊,我看到了误解,函数采用了row.length,它应该提供数组的数量。这就是我在测试中需要用到的。哈哈,谢谢你,我已经很久没有编写ruby脚本了,我只知道基本知识。这只是一次清理还是一个解决方案?呵呵,它也灼伤了我的眼睛。考虑到代码的其余部分,我想更进一步,因为似乎有太多的数据结构分散在一起并从一个转换到另一个。它需要面向对象化,使用领域对象,如产品
类等。
# 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