tags:

views:

364

answers:

2

Hi, as a warning I am completly new to Ruby (been learning for 1 day) so please explain in simple terms.

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
+7  A: 

Use "\n" instead of '\n'

Balon
Thanks for the answer, makes me look like a fool but atleast I now know
babyrats
don't be so hard on yourself: the only way to learn is by asking questions.
glenn jackman
+2  A: 

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
Ben
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.
Carl Smotricz
+1 for that and the nice, auto-closing, idiomatic way to process a file.
Carl Smotricz
Thanks for this. Very helpful.
babyrats