Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在编译器中用Ruby写入文件_Ruby_Learn Ruby The Hard Way - Fatal编程技术网

在编译器中用Ruby写入文件

在编译器中用Ruby写入文件,ruby,learn-ruby-the-hard-way,Ruby,Learn Ruby The Hard Way,我正在翻阅Zed A.Shaw在编译器上的《硬道理学习代码》(Ruby),并坚持练习16 filename = ARGV.first target = File.open(filename, 'w') target.truncate(target.size) target.close() 在控制台中,我键入 run sample.txt 这应该会删除sample.txt文件,但不会 文件sample.txt与开始文件位于同一文件夹中 有什么线索吗?好吧,这不是Ruby的问题(如我所料),而是

我正在翻阅Zed A.Shaw在编译器上的《硬道理学习代码》(Ruby),并坚持练习16

filename = ARGV.first
target = File.open(filename, 'w')
target.truncate(target.size)
target.close()
在控制台中,我键入

run sample.txt
这应该会删除sample.txt文件,但不会

文件sample.txt与开始文件位于同一文件夹中


有什么线索吗?

好吧,这不是Ruby的问题(如我所料),而是编译器的工作方式。通过在没有首先创建sample.txt文件的情况下运行代码,编译器默认情况下在content文件夹中为我创建了该文件。所以把这个。。。可写文件。。。到目录文件夹中,编译器可以向其写入。将它们全部放在同一文件夹(脚本和文件)中会使其无法工作。

要删除内容还是从该位置删除文件?你在哪个站台?是指哪个操作系统?正如doc所说,将文件截断为最多整数字节。必须打开文件进行写入。并非在所有平台上都可用。我正在使用名为Compiler()的在线平台。我用一个文本编辑器在我的Mac终端上试用过,效果很好。所以这可能与编译器有关。我一直在到处寻找我无法写入文件、无法查找和回答的原因。注意:编译器的文本编辑器不会刷新以显示新文件(必须先创建),也不会刷新(必须关闭并重新打开才能查看内容)。