Adding a new line to a file

How do you write a new line in a file in Ruby?

My code for writing to a file:

[Full request code]

print("Word:")                                  #Prompt for word to scrambe
word = gets.chomp                               #Inputs word
wordLength = word.length                        #Gets word length
randomAmount = wordLength * wordLength          #Finds out how many ways to randomize
amountDone = 0                                  #Loop variable for process of randomizing
while amountDone != randomAmount
puts "---------------------------\n"
x = word.chars.to_a.shuffle                 #Changes each character to an array and randomizez them
File.open("output.txt","a+") {|f| f.write(x)}
puts "#{amountDone}:\n #{x}"                
amountDone += 1
puts "---------------------------\n"
end
puts "Type any key to continue..."
endWord = gets.chomp                            #Close program prompt

-

File.open("output.txt","a+") {|f| f.write(x) }

But the problem is that this line ^ is in a loop. This means that it has been repeated many times in reality. Therefore, whenever you open the output file, the output is compressed together. So the program that I wrote basically is that it compresses the words in as many ways as possible. Therefore, if my input was "Abcdefgh", then the output in the file will be displayed as one continuous line:

bdfcegAhhbgfeAcdhcedbAgfdfcAhgebefcdbghAdAfhegbcAegdfhbcbchdgefAhAbedfcgdAfcbhgefhgdAbceefdhcgAbAefbhgcdfAcebdhgAebgchfddhcfAbegcAdfbhgecAgdfhebedAghbfcedbAchgfbhAcgfdeceghAbfddAbfehcgbAhefdgcecfghbdAAhcgdfbedchAgfbebfhAgecdedAhcbgfAfdceghbehdcAbfgcegdhbfAfdAbchgegAhbfecdgeAdhfcbcbdAehfgfhgbcAedchdgbefAfhecdAbgAbedgcfhehcgfbdAAhgcebfdbAcehgfddfchgebAhcAbegdffAbehgcdchdbgAfebeAhgdfcbegcdhfAfecbdhAgdbfehgAcdbcehgfAgdehfcbAbgedAcfhdgcAfehbdfhAgecbcAgdhebfghbAefcdgebhAfdcgecdbAfhgbcAhfedhAbfgdcebAedfhcgbdfchAge

, , - . , , , , , ..

File.open("output.txt","a+") {|f| f.write(x)+" " }

File.open("output.txt","a+") {|f| f.write(x)," " }

File.open("output.txt","a+") {|f| f.write(x)" " }

. , ?

+3
4

, , .

, , . , :

File.open("output.txt","a+") {|f| f.write(x) }

, , :

File.open("output.txt","a+") {|f| f.write(" ") }

:

File.open("output.txt","a+") {|f| f.write(x) }
File.open("output.txt","a+") {|f| f.write(x) }      #This is hilarious

, , , (, 2).

, @seph , , , . , , "puts" "write".

File.open('testfile.txt', 'a+') do |f|
(1..3).each do |i|
f.puts("#{x} #{randomAmount}")
end
end

#change that to

File.open('output.txt', 'a+') do |f|
(randomAmount).each do |i|
f.write("#{amountDone}:\n#{x}")
end
end

, . , , , output.txt, , ( ), .

, !

+2
File.open("output.txt","a+") { |f| f.puts(x) }

Edit:

x :

x + "\n"

2:

. :

File.open('testfile.txt', 'a+') do |f|
  (1..3).each do |i|
    f.puts("test #{i}")
  end
end

(MBP). 3 :

test 1
test 2
test 3
+3

At the simplest level, you can simply add another statement write:

separator = '\n' # or space, tab, etc
...
File.open("output.txt","a+") {|f|
    f.write(x)
    f.write(separator)
}

A combination of two calls writeshould also work:

separator = '\t'
...
File.open("output.txt","a+") {|f| f.puts(x + separator) }
+3
source

You need to put brackets around "", as well as x.

File.open("output.txt","a+") {|f| f.write(x)+" " }

should be changed to

File.open("output.txt","a+") {|f| f.write(x + " ") }
+2
source

All Articles