⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 02 - persisting objects with madeleine.rb

📁 O Reilly Ruby Cookbook source code
💻 RB
字号:
#!/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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -