200

How do I make \n actually work in my output? At the moment it just writes it all in 1 long block. Thanks for any help

Dir.chdir 'C:/Users/name/Music'
music = Dir['C:/Users/name/Music/*.{mp3, MP3}']
puts 'what would you like to call the playlist?'
@new = ''
playlist_name = gets.chomp + '.m3u'

music.each do |z|
  @new += z + '\n'
end

File.open playlist_name, 'w' do |f|
  f.write @new
end

6 Answers 6

395

Use "\n" instead of '\n'

11
  • 12
    Thanks for the answer, makes me look like a fool but atleast I now know
    – babyrats
    Commented Jan 13, 2010 at 21:18
  • 94
    don't be so hard on yourself: the only way to learn is by asking questions. Commented Jan 13, 2010 at 21:58
  • 16
    @babyrats - u not the only one :)
    – Ninad
    Commented Sep 14, 2011 at 10:57
  • 7
    It seems that both Ruby and PHP do not expand escape sequences in single quoted strings.
    – kjagiello
    Commented Dec 31, 2013 at 15:02
  • 3
    "\n" is newline, '\n\ is literally backslash and n.
    – mahemoff
    Commented Oct 20, 2015 at 0:05
14

I would like to share my experience with \n
I came to notice that "\n" works as-

puts "\n\n" // to provide 2 new lines

but not

p "\n\n"

also puts '\n\n'
Doesn't works.

Hope will work for you!!

12

You can do this all in the File.open block:

Dir.chdir 'C:/Users/name/Music'
music = Dir['C:/Users/name/Music/*.{mp3, MP3}']
puts 'what would you like to call the playlist?'
playlist_name = gets.chomp + '.m3u'

File.open playlist_name, 'w' do |f|
  music.each do |z|
    f.puts z
  end
end
2
  • 2
    I guess one interesting and useful thing to take away from this is that puts outputs a string and an "automatic" trailing line break; that's handier than appending it in code. Commented Jan 13, 2010 at 21:12
  • +1 for that and the nice, auto-closing, idiomatic way to process a file. Commented Jan 13, 2010 at 21:13
5

Actually you don't even need the block:

  Dir.chdir 'C:/Users/name/Music'
  music = Dir['C:/Users/name/Music/*.{mp3, MP3}']
  puts 'what would you like to call the playlist?'
  playlist_name = gets.chomp + '.m3u'

  File.open(playlist_name, 'w').puts(music)
1

For me it didn't work with adding "\n" to the end of an existing argument to puts, so as a workaround I called print on the next line with "\n" as an argument, although I suppose I could have just called puts again.

This did not produce the desired result:

puts "Coach says: #{coach_answer(user_input)}\n"

But this did:

puts "Coach says: #{coach_answer(user_input)}"
print "\n"
1

None of the methods mentioned above worked for me, possibly because I was trying to use Ruby on a different platform (Appcircle). Ultimately, adding a space before \n solved the issue. For example:

puts " \nMy new line message."
1
  • 1
    IMO, it's specifically related to the Appcircle's build log viewer. Just using puts "\n" works fine for a vanilla Ruby execution, but it behaves weirdly in the Appcircle build log viewer. But using a white space in front of the new line has no side effect on live build logs and their downloaded versions. So, specifically for the Appcircle platform, your suggestion can be used safely. Commented Dec 1 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.