How to do a newline in output

前端 未结 4 1862
时光取名叫无心
时光取名叫无心 2020-12-23 00:02

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         


        
相关标签:
4条回答
  • 2020-12-23 00:44

    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)
    
    0 讨论(0)
  • 2020-12-23 00:48

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

    0 讨论(0)
  • 2020-12-23 00:58

    Use "\n" instead of '\n'

    0 讨论(0)
  • 2020-12-23 00:58

    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
    
    0 讨论(0)
提交回复
热议问题