irbrc.d/rates.rb
def rate(options = {})
  clear = `clear`
  defaults = {
    :delay => 1,
    :width => 5,
    :clear => true
  }
  options = defaults.merge(options)
  values = []
  loop do
    values << [yield, Time.now]
    values = values.last(options[:width])
    value_delta = values.last.first - values.first.first
    time_delta = values.last.last - values.first.last
    rate = (value_delta.to_f / time_delta)
    rate = 0 unless rate.finite?
    last_value = values.last.first
    print clear if options[:clear]
    time_to_zero = 
      if rate != 0 && last_value != 0 && (rate.abs / rate) != (last_value.abs / last_value)
        "%.3f hours" % (-last_value.to_f / rate / 60 / 60)
      end
    
    puts "%15s (%s/sec) #{time_to_zero}" % [rate_delimiter(last_value), rate_delimiter("%.3f" % rate)]
    puts unless options[:clear]
    sleep options[:delay]
  end
end
# Watches a value change over time showing you current value and rate of
# change.
#
#   rate { Time.now.to_i }
#   # 1243457076 (0.999/sec) 
#   # 1243457077 (0.999/sec) 
#   # 1243457078 (0.999/sec) 
Watches a value change over time showing you current value and rate of
change.


Watches a value change over time showing you current value and rate of change .
def rate(options = {})
  clear = `clear`
  defaults = {
    :delay => 1,
    :width => 5,
    :clear => true
  }
  options = defaults.merge(options)
  values = []
  loop do
    values << [yield, Time.now]
    values = values.last(options[:width])
    value_delta = values.last.first - values.first.first
    time_delta = values.last.last - values.first.last
    rate = (value_delta.to_f / time_delta)
    rate = 0 unless rate.finite?
    last_value = values.last.first
    print clear if options[:clear]
    time_to_zero = 
      if rate != 0 && last_value != 0 && (rate.abs / rate) != (last_value.abs / last_value)
        "%.3f hours" % (-last_value.to_f / rate / 60 / 60)
      end
    puts "%15s (%s/sec) #{time_to_zero}" % [rate_delimiter(last_value), rate_delimiter("%.3f" % rate)]
    puts unless options[:clear]
    sleep options[:delay]
  end
end
def rate ( options = { } ) clear = ` clear ` defaults = { :delay => 1 , :width => 5 , :clear => true } options = defaults . merge ( options ) values = [ ] loop do values << [ yield , Time . now ] values = values . last ( options [ :width ] ) value_delta = values . last . first - values . first . first time_delta = values . last . last - values . first . last rate = ( value_delta . to_f / time_delta ) rate = 0 unless rate . finite? last_value = values . last . first print clear if options [ :clear ] time_to_zero = if rate != 0 && last_value != 0 && ( rate . abs / rate ) != ( last_value . abs / last_value ) "%.3f hours" % ( - last_value . to_f / rate / 60 / 60 ) end puts "%15s (%s/sec) #{time_to_zero}" % [ rate_delimiter ( last_value ) , rate_delimiter ( "%.3f" % rate ) ] puts unless options [ :clear ] sleep options [ :delay ] end end
Watches a value change over time showing you current value and rate of
change.
Watches a value change over time showing you current value and rate of change .
options