Skip to content

Instantly share code, notes, and snippets.

@gevans
Created March 15, 2014 23:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gevans/9575816 to your computer and use it in GitHub Desktop.
Save gevans/9575816 to your computer and use it in GitHub Desktop.
Extension to Ruby's Numeric for easy conversion between hash rate units (adapted from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/numeric/bytes.rb).
##
# Example:
#
# 123.kilohashes + 1.hash * 1.megahash
# => 1123000
#
class Numeric
KILOHASH = 1000
MEGAHASH = KILOHASH * 1000
GIGAHASH = MEGAHASH * 1000
TERAHASH = GIGAHASH * 1000
PETAHASH = TERAHASH * 1000
EXAHASH = PETAHASH * 1000
def hashes
self
end
alias hash hashes
def kilohashes
self * KILOHASH
end
alias kilohash kilohashes
def megahashes
self * MEGAHASH
end
alias megahash megahashes
def gigahashes
self * GIGAHASH
end
alias gigahash gigahashes
def terahashes
self * TERAHASH
end
alias terahash terahashes
def petahashes
self * PETAHASH
end
alias petahash petahashes
def exahashes
self * EXAHASH
end
alias exahash exahashes
end
@gevans
Copy link
Author

gevans commented May 16, 2014

Scratch that. Overriding Object#hash is a really bad idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment