Skip to content

Instantly share code, notes, and snippets.

@adamloo85
Last active December 28, 2015 07:09
Show Gist options
  • Save adamloo85/7462522 to your computer and use it in GitHub Desktop.
Save adamloo85/7462522 to your computer and use it in GitHub Desktop.
Implementing a "hash-like" data structure using arrays. Unit tests through the Shoulda gem.
require 'rubygems'
require 'shoulda-context'
class HashFake
def initialize(size=0)
@body = Array.new(size)
end
def body
@body
end
def insert(key, value)
key = make_key(key)
pair = [key, value]
check_key(key) ? change_key(key, value) : @body << pair
end
def find_value(key)
check_key(key) ? @body[index(key)][1] : nil
end
def delete_pair(key)
check_key(key) ? @body.delete_at(index(key)) : nil
end
private
def make_key(key)
key = key.to_sym unless key.instance_of? Symbol
end
def check_key(key)
@body.each { |k, v| return true if k == key }
return false
end
def change_key(key, value)
@body[index(key)][1] = value
end
def index(key)
index = @body.find_index {|k, v| k == key}
end
end
class HashTest < Test::Unit::TestCase
context "hash" do
setup do
@hash = HashFake.new
end
should "have the correct class" do
assert_equal HashFake, @hash.class
end
context "adding, deleting, fetching" do
should ""
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment