Skip to content

Instantly share code, notes, and snippets.

@bankair
Created July 12, 2016 07:56
Show Gist options
  • Select an option

  • Save bankair/b899495ae6d2810a55139cb49f3c4ddf to your computer and use it in GitHub Desktop.

Select an option

Save bankair/b899495ae6d2810a55139cb49f3c4ddf to your computer and use it in GitHub Desktop.
A timestamping helper in ruby
class Chrono < Hash
attr_accessor :last, :section, :total
def initialize(new_section = :unknown)
super(0.0)
self.last = Time.now
self.section = new_section
self.total = 0.0
end
def top(new_section = :unknown)
t = Time.now
elapsed = t - last
self[section] = elapsed
self.total += elapsed
self.last = t
self.section = new_section
end
def report
{ total: total, report: self }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment