Created
November 13, 2014 08:37
-
-
Save skv-headless/a399cbc8f02895f3062e to your computer and use it in GitHub Desktop.
simple hash
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 HashTable | |
def initialize | |
@bins = Array.new(11) | |
end | |
def [](key) | |
@bins[bin_num(key)] | |
end | |
def []=(key, value) | |
@bins[bin_num(key)] = value | |
end | |
def bin_num(key) | |
key.hash % @bins.length | |
end | |
end | |
hash = HashTable.new | |
hash[:hello] = 123 | |
p hash[:hello] | |
p hash['hello'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment