How to write to file in Ruby?

I need to read the data out of database and then save it in a text file.

How can I do that in Ruby? Is there any file management system in Ruby?


Ruby File类将为您提供::new::open来龙去脉,但它的父类IO类深入到#read#write


你在寻找以下吗?

File.open(yourfile, 'w') { |file| file.write("your text") }

You can use the short version:

File.write('/path/to/file', 'Some glorious content')

It returns the length written; see ::write for more details and options.

链接地址: http://www.djcxy.com/p/4362.html

上一篇: Ruby中的行注释?

下一篇: 如何在Ruby中写入文件?