02 - persisting objects with madeleine.rb

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

RB
71
字号
#!/usr/bin/ruby -w # poll.rbrequire 'rubygems'require 'madeleine'class Poll  attr_accessor :name  attr_reader :total  def initialize(name)    @name = name    @total = 0  end  def agree    @total += 1  end  def disagree    @total -= 1  endend#---poll = SnapshotMadeleine.new('poll_data') do  Poll.new('Is Ruby great?')end#---if ARGV[0] == 'agree'  poll.system.agreeelsif ARGV[0] == 'disagree'  poll.system.disagreeendputs "Name: #{poll.system.name}"puts "Total: #{poll.system.total}"#---poll.take_snapshot#---$ ruby poll.rb agreeName: Is Ruby great?Total: 1$ ruby poll.rb agreeName: Is Ruby great?Total: 2$ ruby poll.rb disagreeName: Is Ruby great?Total: 1#---poll = SnapshotMadeleine.new('poll_data') do  Poll.new('Is Ruby great?')end#---$ ls poll_data000000000000000000001.snapshot000000000000000000002.snapshot000000000000000000003.snapshot#---at_exit { poll.take_snapshot }#---def save_recurring_snapshots(madeleine_object, time_interval)  loop do    madeleine_object.take_snapshot    sleep time_interval  endendThread.new { save_recurring_snapshots(poll, 24*60*60) }#---

⌨️ 快捷键说明

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