Created
February 11, 2015 17:04
-
-
Save fellipebrito/40507770464bde86ebab to your computer and use it in GitHub Desktop.
Example of how to use hashid
This file contains 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 HashMachine | |
## Source: https://github.com/peterhellberg/hashids.rb | |
## In this example I'm using 11 as the size of the hash. This is the same size youtube uses | |
require 'hashids' | |
@@my_salt = 'this is my salt' | |
@@my_hash_size = 11 | |
def hashids | |
hashids = Hashids.new @@my_salt, @@my_hash_size | |
end | |
def encode number | |
hashids.encode number | |
end | |
def decode hash | |
hashids.decode hash | |
end | |
end | |
hash_machine = HashMachine.new | |
hash = hash_machine.encode 12345 | |
p "My hash is: #{hash}" | |
p "My number is: #{hash_machine.decode hash}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment