I want to output data(integers) onto a file called stdout.txt. The trouble seems to be that my code overwrites the existing data in the file instead of adding to it line by line.
if failPlaces.empty? == false puts "position: #{failPlaces.last}" output = File.open( "stdout","w" ) output << "#{failPlaces.last}\n" output.close else puts "He Passes it!!!!!!!!!!!!!!!!!" output = File.open( "stdout","w" ) output << "Pass\n" output.close end I would like to understand why my code is behaving like this and what the solution would be.
File.open:File.open( "stdout","w" ) { |output| output << "#{failPlaces.last}\n" }. Your file will be opened and closed for you automatically.