Created
July 12, 2016 07:56
-
-
Save bankair/b899495ae6d2810a55139cb49f3c4ddf to your computer and use it in GitHub Desktop.
A timestamping helper in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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