【发布时间】:2011-12-23 14:55:34
【问题描述】:
我需要读入一个包含数字列表的文件。
此代码读取文件并将其放入二维数组。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为 int。任何想法将to_i 方法放在哪里?
Class Terrain
def initialize file_name
@input = IO.readlines(file_name) #read in file
@size = @input[0].to_i
@land = [@size]
x = 1
while x <= @size
@land << @input[x].split(/\s/)
x += 1
end
#puts @land
end
end
【问题讨论】:
-
您之前的问题发生了什么? stackoverflow.com/questions/7993821。它非常相似,但是您没有提供任何反馈,没有 cmets,没有投票,什么都没有。那里的解决方案展示了如何使用地图,但您在这个问题中继续使用非惯用语。
-
为了补充@tokland 的评论,如果您不明白答案,在 cmets 中要求澄清是完全可以接受的。
标签: ruby int type-conversion