tags:

views:

221

answers:

3

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
+2  A: 

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.

Ivan
`system("cat > test_file << \"#{a}\"")' produces an empty file, `system("echo \"#{a}\" >> testfile")' produces a solution, but your communication of the idea is hard to understand of itself.
Jesse
+3  A: 

Write a to a file called "testfile":

File.open("testfile", "w") do |io| io.print a done
JesperE
File.open("testfile", "w") do |f| f << "formatted text block" end ..... works. thanks :-)
Jesse
A: 

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
    |f|
    f.write("line1\nline2\n")
end
Glomek
really? who else has covered it?
Jesse
and what's a process?
Jesse
Sorry to be blunt, but that's like a car mechanic asking "what's an engine?"
JesperE