我想在Ruby中写入文件:
File.open(yourfile, 'w')
我想将文本推送到此文件中,但要使用格式,例如:
Title
subheading
some text here
subtext here
-------------------------------------------
sometext here
subtext here
-------------------------------------------
不知道如何将这种格式写入文件?
您可以先将字符串格式化,然后再将其保存到文件中,并保留格式:
string = <<-TXT
#{title}
#{subheading}
#{text1}
#{subtext1}
-------------------------------------------
#{text2}
#{subtext2}
------------------------------------------
TXT
File.open("my file", 'w') { |f| f.write(string) }
虽然如果您尝试智能地将文本居中(例如标题和副标题),则需要使用格式化程序,就像printf
其他人建议的那样
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句