lakam99
lakam99

Reputation: 605

Adding a newline to a file

How do you write a newline to a file in Ruby?

My code to write to the file is:

[Full code request]

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 that line ^ is in a loop. Meaning it's repeated over and over again quite a lot of times actually. So whenever you open the output file, the output is squashed together. So what the program I've wrote basically does is it scrambles words into as many ways as possible. So if my input was "Abcdefgh", then the output in the file is going to be displayed as one continuous line:

bdfcegAhhbgfeAcdhcedbAgfdfcAhgebefcdbghAdAfhegbcAegdfhbcbchdgefAhAbedfcgdAfcbhgefhgdAbceefdhcgAbAefbhgcdfAcebdhgAebgchfddhcfAbegcAdfbhgecAgdfhebedAghbfcedbAchgfbhAcgfdeceghAbfddAbfehcgbAhefdgcecfghbdAAhcgdfbedchAgfbebfhAgecdedAhcbgfAfdceghbehdcAbfgcegdhbfAfdAbchgegAhbfecdgeAdhfcbcbdAehfgfhgbcAedchdgbefAfhecdAbgAbedgcfhehcgfbdAAhgcebfdbAcehgfddfchgebAhcAbegdffAbehgcdchdbgAfebeAhgdfcbegcdhfAfecbdhAgdbfehgAcdbcehgfAgdehfcbAbgedAcfhdgcAfehbdfhAgecbcAgdhebfghbAefcdgebhAfdcgecdbAfhgbcAhfedhAbfgdcebAedfhcgbdfchAge

So, what I want to do is I want to have some sort of separation between the output. So every time it's outputted there is either a new line, a space, a pipe, a slash, etc.

I've already tried

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

and

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

and

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

but it doesn't work. So how might I be able to solve this?

Upvotes: 4

Views: 12193

Answers (4)

lakam99
lakam99

Reputation: 605

When I figured out this solution I actually laughed at how ridiculously simple it was.

All I really had to do was add the spacing separately along with the line. So for:

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

all that needed to be done was to add this line:

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

so it would be:

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

So the solution was quite simple all along, although if there are more simple solutions(perhaps code on one line instead of 2).

I think @seph's answer actually worked though, but still in the same way where it wrote it letter by letter then a space. I think it would write each individually if you changed "puts" to "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

and then I think it works. I don't know for certain since the time I tried this it actually worked or not because I had misplaced my output.txt file and so I assumed the empty one in my home directory had the output (which wasn't true) so I got the wrong idea.

But thanks to all who tried!

Upvotes: 3

bta
bta

Reputation: 45057

On the simplest level you can simply add another write statement:

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

Combining the two write calls should work as well:

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

Upvotes: 3

Andrew Grimm
Andrew Grimm

Reputation: 81480

You need to put the parenthesis around the " " as well as the x.

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

should be changed to

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

Upvotes: 2

seph
seph

Reputation: 6076

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

should do it

Edit:

You could append a newline to x like this:

x + "\n"

Edit 2:

You want open the file outside of the loop. Try this:

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

It works on my machine(MBP). It puts 3 lines in the file:

test 1
test 2
test 3

Upvotes: 5

Related Questions