📄 06 - writing to a file.rb
字号:
open('output', 'w') { |f| f << "This file contains great truths.\n" }open('output', 'w') do |f| f.puts 'The great truths have been overwritten with an advertisement.'endopen('output') { |f| f.read }# => "The great truths have been overwritten with an advertisement.\n"#---open('output', "a") { |f| f.puts 'Buy Ruby(TM) brand soy sauce!' }open('output') { |f| puts f.read }# The great truths have been overwritten with an advertisement.# Buy Ruby(TM) brand soy sauce!#---open('output', 'w') do |f| [1,2,3].each { |i| f << i << ' and a ' }endopen('output') { |f| f.read } # => "1 and a 2 and a 3 and a "#---open('output', 'w') do |f| f << 'This is going into the Ruby buffer.' f.flush # Now it's going into the OS buffer.endIO.sync = falseopen('output', 'w') { |f| f << 'This is going straight into the OS buffer.' }#---
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -