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
Was it helpful?


You need to quote the interpolated parameter:

system("cat > test_file << \"#{a}\"")

And, cat is expecting a filename, not some text to append to test_file, so, this would work as I think you intended:

system("echo \"#{a}\" >> test_file")

If you want to do this in pure Ruby let me know and I'll give you an example.


Write a to a file called "testfile":"testfile", "w") do |io| io.print a done

Writing to a file directly has already been covered by JesperE. To write to a process (in this case a "cat" process) use popen.

IO.popen("cat > foo", "w") do
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top