不知道这里发生了什么,或者在这种情况下可能是整数.这是代码:
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
Run Code Online (Sandbox Code Playgroud)
filename是一个字符串,我已经检查以确保路径有效.
有什么想法吗?谢谢.
File.read没有模式或块的第二个参数,即File.open:
contents_string = File.read(File.expand_path('lib/project_euler/' + filename))
Run Code Online (Sandbox Code Playgroud)
请注意,您还可以写:
contents = File.open(path).lines # returns a lazy enumerator, keeps the file open
Run Code Online (Sandbox Code Playgroud)
要么:
contents = File.readlines(path) # returns an array, the file is closed.
Run Code Online (Sandbox Code Playgroud)