03 - checking when a file was last used.rb

来自「O Reilly Ruby Cookbook source code」· RB 代码 · 共 52 行

RB
52
字号
open("output", "w") { |f| f << "Here's some output.\n" }stat = File.stat("output")stat.mtime                               # => Thu Mar 23 12:23:54 EST 2006stat.atime                               # => Thu Mar 23 12:23:54 EST 2006sleep(2)open("output", "a") { |f| f << "Here's some more output.\n" }stat = File.stat("output")stat.mtime                               # => Thu Mar 23 12:23:56 EST 2006stat.atime                               # => Thu Mar 23 12:23:54 EST 2006sleep(2)open("output") { |f| contents = f.read }stat = File.stat("output")stat.mtime                               # => Thu Mar 23 12:23:56 EST 2006stat.atime                               # => Thu Mar 23 12:23:58 EST 2006#---def save_game(file)  score = 1000  open(file, "w") do |f|    f.puts(score)    f.puts(Time.new.to_i)  endend#---def load_game(file)  open(file) do |f|    score = f.readline.to_i    time = Time.at(f.readline.to_i)    difference = (File.stat(file).mtime - time).abs        raise "I suspect you of cheating." if difference > 1    "Your saved score is #{score}."  end  end#---save_game("game.sav")sleep(2)load_game("game.sav")# => "Your saved score is 1000."# Now let's cheat by increasing our score to 9000open("game.sav", "r+b") { |f| f.write("9") }load_game("game.sav")# RuntimeError: I suspect you of cheating.#---

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?