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.

Revisions

  1. adamloo85 renamed this gist Nov 14, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. adamloo85 renamed this gist Nov 14, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. adamloo85 created this gist Nov 14, 2013.
    61 changes: 61 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    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