【发布时间】:2017-01-10 06:16:44
【问题描述】:
我使用的是 Ruby 2.4。我在创建数组数组时遇到问题。我有这个
data_cols = [[], []]
lines.each do |line|
parts = [0, *shared_space_indexes, line.size].each_cons(2).map { |a, b| line[a...b].strip }
parts.each_with_index do |part, index|
data_cols[index][data_cols[index] ? data_cols[index].size : 0] = part
end
end
但我在“data_cols[index][data_cols[index] ? data_cols[index].size : 0] = part”行收到“NoMethodError: undefined method `[]=' for nil:NilClass”错误。我想要做的是对于每个部件数组,将“部件”中的每个项目推到它自己的数组上,对应于部件中该元素的索引。因此,例如,如果循环的第一次迭代的部分等于
[1, 5, 8, 12]
我会有一个看起来像的 data_cols 数组
[[1], [5], [8], [12]]
如果循环的下一次迭代有一个看起来像这样的部件数组
[19, 20, 21, 22]
data_cols 数组随后将更改为
[[1, 19], [5, 20], [8, 21], [12, 22]]
【问题讨论】:
标签: arrays ruby nosuchmethoderror