Last active
August 29, 2015 14:22
-
-
Save ybur-yug/48c114943b1875ba453d to your computer and use it in GitHub Desktop.
git database
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 GitDatabase | |
attr_accessor :items | |
def initialize | |
`git init` | |
@items = {} | |
end | |
def save(key, input) | |
set(key, hash_object(input.to_s)) | |
end | |
def find(key) | |
cat_file(@items[key]) | |
end | |
def set(key, hash) | |
@items[key] = hash | |
end | |
def hash_object(string) | |
`echo #{string} | git hash-object -w --stdin`.strip! | |
end | |
def cat_file(hash) | |
`git cat-file -p #{hash}` | |
end | |
end | |
def test | |
a = GitDatabase.new | |
a.save("Eggs", 12) | |
a.save(10420, 13) | |
a.save(:symstuff, 14) | |
a.save(0.1, 132) | |
puts a.find(0.1) | |
puts a.find(:symstuff) | |
puts a.find(10420) | |
puts a.find("Eggs") | |
end | |
test | |
# => 132 | |
# 14 | |
# 13 | |
# 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment