2011-06-02 13 views

回答

1

如果percent_finished是一個ActiveRecord模型的一部分,你其實可以只需打個電話給percent_finished_changed?

例如:

puts "#{percent_finished.round}% uploading complete" if percent_finished_changed? 

下面是一些文檔:

http://ar.rubyonrails.org/classes/ActiveRecord/Dirty.html

+0

如果我不使用Rails? – emurad 2011-06-02 21:11:13

+0

然後你可以創建你自己的'percent_finished_changed?'方法。一種方法可能是在初始化時將初始的'percent_finished'值放入一個實例變量中(即'def initialize; @orig_pf = pf; end'),然後通過直接比較在新的'percent_finished_changed?'方法中檢查該值(即'def pf_changed ?; @orig_pf!= pf; end') – jerhinesmith 2011-06-02 21:14:32

+0

那麼沒有一個行解決方案? – emurad 2011-06-02 21:41:10

1

這是一個命令行工具?

last_percent = nil 
upload_loop do 
    # ... 
    percent_rounded = percent_finished.round 
    puts "#{percent_rounded}% uploading complete" if percent_rounded != last_percent 
    last_percent = percent_rounded 
end 
相關問題