如何在输出中做一个换行符
我如何使\n
实际上在我的输出中工作? 目前它只是把它写在一个长块。 谢谢你的帮助
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
使用"\n"
而不是'\n'
您可以在File.open块中完成这一切:
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
其实你甚至不需要块:
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)