正確理解Ruby更新文件
作者:佚名
Ruby語言作為一種新型編程語言,有很多不同于其他我們熟知的編程語言的操作方法。希望通過這篇文章的介紹大家可以了解到它特有的一些性質。
作為一個優秀的編程人員,我們必須要不斷的去學習新知識來補充我們的知識庫,更新技術。Ruby語言就是一項新的編程語言,值得我們去深入學習。在這里我們將會了解到Ruby更新文件的一些操作技巧。#t#
Ruby更新文件
假設我們想要打開一個文件用于讀和寫,簡單的加一個'+'號到file mode就行了:
- f1 = File.new("file1", "r+")
- # Read/write, starting at
beginning of file. - f2 = File.new("file2", "w+")
- # Read/write; truncate
existing file or create a new one. - f3 = File.new("file3", "a+")
- # Read/write; start at
end of existing file or create a - # new one.
Ruby更新文件中的追加一個文件
假設我們想要追加一段信息到一個存在文件,當我們打開文件時使用'a'作為file mode就行了:
- logfile = File.open
("captains_log", "a")- # Add a line at the
end, then close.- logfile.puts "Stardate
47824.1: Our show has
been canceled."- logfile.close
以上就是我們對Ruby更新文件的一些操作方法介紹。
責任編輯:曹凱
來源:
ddvip.com