我之前一直是一名 Ruby 程序员,最近开始学习 Crystal。但是我发现关于 File 类的资料很少,我想要打开和读取一个文件,但是却遇到了错误。
file = File.open("ditto.txt")
file = file.read
tequila@tequila-pc:~/code$ crystal fileopen.cr
Error in fileopen.cr:2: wrong number of arguments for 'File#read' (given 0, expected 1)
Overloads are:
- IO::Buffered#read(slice : Bytes)
- IO#read(slice : Bytes)
file = file.read
^~~~
IO#read(slice : Bytes)
-File
从IO继承了read
方法。根据IO#read
的文档,它需要一个参数。您可能想尝试使用File.read
类方法。 - Stefan