不确定这里发生了什么,或者在这种情况下什么是整数代码如下:
def build_array_from_file(filename)
contents = []
File.read(File.expand_path('lib/project_euler/' + filename), 'r') do |file|
while line = file.get
contents << line
end
end
contents
end
filename是一个字符串,我已经检查了路径是否有效。
有什么想法吗?谢谢。
最佳答案
File.read没有模式或块的第二个参数,即File.open:
contents_string = File.read(File.expand_path('lib/project_euler/' + filename))
请注意,您还可以编写:
contents = File.open(path).lines # returns a lazy enumerator, keeps the file open
或:
contents = File.readlines(path) # returns an array, the file is closed.
关于ruby - 尝试在Ruby中打开文件-获取TypeError:无法将String转换为Integer,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11109440/