如何使
\n
在我的输出中真正发挥作用?目前它只是将其全部写入 1 个长块中。感谢您的帮助
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'
我想与
\n
puts "\n\n" // to provide 2 new lines
但不是
p "\n\n"
还有 放
'\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)
对我来说,添加“不起作用” " 到
puts
的现有参数末尾,因此作为解决方法,我在下一行用 " 调用 print
“作为一个论点,尽管我想我可以再次打电话给puts
。
这没有产生预期的结果:
puts "Coach says: #{coach_answer(user_input)}\n"
但这做到了:
puts "Coach says: #{coach_answer(user_input)}"
print "\n"
上面提到的方法都不适合我,可能是因为我试图在不同的平台(Appcircle)上使用 Ruby。最终,在
\n
之前添加一个空格解决了问题。例如:
puts " \nMy new line message."