无法将 Ruby 块转换为文件
请帮忙,我被困在这里了---
irb> a = "line of text\n line two\n line three"
irb> system("cat > test_file << #{a}")
cat: of: No such file or directory
cat: text: No such file or directory
=> false
Please help, I am stuck here ---
irb> a = "line of text\n line two\n line three"
irb> system("cat > test_file << #{a}")
cat: of: No such file or directory
cat: text: No such file or directory
=> false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将 a 写入名为“testfile”的文件:
Write a to a file called "testfile":
您需要引用插值参数:
并且, cat 需要一个文件名,而不是附加到 test_file 的一些文本,因此,这将按照我认为您的意图工作:
如果您想在纯 Ruby 中执行此操作,请告诉我,我'给你举个例子。
You need to quote the interpolated parameter:
And, cat is expecting a filename, not some text to append to test_file, so, this would work as I think you intended:
If you want to do this in pure Ruby let me know and I'll give you an example.
JesperE 已经涵盖了直接写入文件。 要写入进程(在本例中为“cat”进程),请使用 popen。
Writing to a file directly has already been covered by JesperE. To write to a process (in this case a "cat" process) use popen.