How to do a newline in output

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

Answers


Use "\n" instead of '\n'


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

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)

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!!


Need Your Help

How can I get the name of function inside a JavaScript function?

javascript function typeof

How is it possible to learn the name of function I am in?

Calling a method from another controller

ruby-on-rails

If I've got a method in a different controller to the one I'm writing in, and I want to call that method, is it possible, or should I consider moving that method to a helper?