使用Ruby在Compilr中写入文件

时间:2022-07-23 00:04:36

I'm banging through Zed A. Shaw's learn code the hard way (Ruby) on compilr and am stuck on exercise 16.

我正在敲打Zed A. Shaw在编译器上学习代码(Ruby),并坚持练习16。

filename = ARGV.first
target = File.open(filename, 'w')
target.truncate(target.size)
target.close()

In the console, I type

在控制台中,我输入

run sample.txt

This should wipe the sample.txt file, but it doesn't.

这应该擦除sample.txt文件,但它不会。

The file, sample.txt is in the same folder as the Start file.

文件sample.txt与Start文件位于同一文件夹中。

Any clues?

1 个解决方案

#1


1  

Ok it's not the Ruby issue (as I expected) it's to do with how Compilr works. By running the code without having first created a sample.txt file, Compilr created the file for me by default in the content folder. So putting the uh... writable files... into the contents folder enables Compilr to write to them. Putting them all in the same folder (the script and the files) makes it not work.

好吧,这不是Ruby问题(正如我所料),它与Compilr的工作方式有关。通过在没有首先创建sample.txt文件的情况下运行代码,Compilr默认在内容文件夹中为我创建了该文件。因此,将呃...可写文件...放入内容文件夹可以使Compilr写入它们。将它们全部放在同一个文件夹(脚本和文件)中使它无法正常工作。

#1


1  

Ok it's not the Ruby issue (as I expected) it's to do with how Compilr works. By running the code without having first created a sample.txt file, Compilr created the file for me by default in the content folder. So putting the uh... writable files... into the contents folder enables Compilr to write to them. Putting them all in the same folder (the script and the files) makes it not work.

好吧,这不是Ruby问题(正如我所料),它与Compilr的工作方式有关。通过在没有首先创建sample.txt文件的情况下运行代码,Compilr默认在内容文件夹中为我创建了该文件。因此,将呃...可写文件...放入内容文件夹可以使Compilr写入它们。将它们全部放在同一个文件夹(脚本和文件)中使它无法正常工作。