How do I save the text of puts in Ruby to a txt file?
Asked Answered
M

2

5

I wrote a madlib in Ruby, and want to save the resulting madlib to a txt file. This is what I wrote, but the resulting txt file is empty:

file=File.open("madlib_output.txt","a")
file.puts
file.close
Monday answered 12/9, 2014 at 21:18 Comment(2)
The file shouldn't be empty. There should be a single empty line in it. Otherwise something in your Ruby installation is severely broken.Spectacles
You're right. I misspoke. There is, indeed, one single, yet disappointingly empty line in the out file. Le sigh.Monday
H
13

There are ways to save the output of a script to a file without having to modify every puts in the script.

The easiest is to route the output at the command-line using redirection. Running a script with > some_file at the of the command will route all STDOUT to the file. Similarly, using > some_file 2>&1 will route both STDOUT and STDERR to the same file. This won't capture anything typed in at a gets as the code waits for input though, because that won't count as program output.

If you don't mind changing your code a little, you can temporarily change the interpreter's idea of what STDOUT is by reassigning it to a file:

old_stdout = $stdout
File.open('output.txt', 'w') do |fo|
  $stdout = fo

  # ----
  # your code goes here
  puts "hello world"
  # ----

end
$stdout = old_stdout

Run that, then look at the file "output.txt" and you'll see "hello world", even though we didn't print to the file-handle fo directly, like we would normally do using fo.puts.

There are a variety of ways of doing the same thing but they amount to pointing STDOUT or STDERR somewhere else, writing to them, then resetting them.

Typically, if we intend from the start to output to a file, then we should use a File.open block:

File.open('output.txt', 'w') do |fo|
  fo.puts "hello world"
end

The benefit of that is the file will be closed automatically when the block exits.

Houseline answered 12/9, 2014 at 21:40 Comment(1)
I see what this did. It changes the standardout of the questions that are printed to the screen to being printed to the file. The way that I wrote the madlib in ruby is so that questions are printed to the screen, the user puts in an answer, that happens 10 or so times, and then the gets populates the puts with the user's answers. The standard out for the puts is the screen. I think that I am just too much of a Ruby novice at this to craft code that will allow the user to both see the populated madlib, and for that madlib to be saved to a file.Monday
C
6

Is this what you looking for ? You can open madlib_output.txt file in append mode and whatever you want to write will be inside the block eg: "hi"

File.open("madlib_output.txt","a") do |f|
 f.puts "hi"
end
Collate answered 12/9, 2014 at 21:23 Comment(1)
in this case...it opens existing madlib_output file and appends "HI" to it..is this the scenario you are looking for ?Collate

© 2022 - 2024 — McMap. All rights reserved.