Skip to content

Commit

Permalink
Finished Hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
cisenor committed Mar 1, 2018
1 parent 93b5f79 commit 85af231
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .path_progress
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6,6,6,6,6,6,7,6,6,7,7,7,7,7,8,8,9,10,10,11,10,10,11,12,13,13,13,13,13,13,15,16,17,17,18,18,18,18,19,19,19,20,20,20,21,22,22,23,25,27,27,28,29,30,31
6,6,6,6,6,6,7,6,6,7,7,7,7,7,8,8,9,10,10,11,10,10,11,12,13,13,13,13,13,13,15,16,17,17,18,18,18,18,19,19,19,20,20,20,21,22,22,23,25,27,27,28,29,30,31,31,32,33,34,34,34,35,34,36,37,37,38,39,40,40,40,40,41,41,41,41,42,43
72 changes: 37 additions & 35 deletions about_hashes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,91 +3,93 @@
class AboutHashes < Neo::Koan
def test_creating_hashes
empty_hash = Hash.new
assert_equal __, empty_hash.class
assert_equal(__, empty_hash)
assert_equal __, empty_hash.size
assert_equal Hash, empty_hash.class
assert_equal({}, empty_hash)
assert_equal 0, empty_hash.size
end

def test_hash_literals
hash = { :one => "uno", :two => "dos" }
assert_equal __, hash.size
assert_equal 2, hash.size
end

def test_accessing_hashes
hash = { :one => "uno", :two => "dos" }
assert_equal __, hash[:one]
assert_equal __, hash[:two]
assert_equal __, hash[:doesnt_exist]
assert_equal "uno", hash[:one]
assert_equal "dos", hash[:two]
assert_equal nil, hash[:doesnt_exist]
end

def test_accessing_hashes_with_fetch
hash = { :one => "uno" }
assert_equal __, hash.fetch(:one)
assert_raise(___) do
assert_equal "uno", hash.fetch(:one)
assert_raise(KeyError) do
hash.fetch(:doesnt_exist)
end

# THINK ABOUT IT:
#
# Why might you want to use #fetch instead of #[] when accessing hash keys?
# Why might you want to use #fetch instead of #[] when accessing hash keys?
# [] won't throw errors.
end

def test_changing_hashes
hash = { :one => "uno", :two => "dos" }
hash[:one] = "eins"

expected = { :one => __, :two => "dos" }
assert_equal __, hash
expected = { :one => "eins", :two => "dos" }
assert_equal expected, hash

# Bonus Question: Why was "expected" broken out into a variable
# rather than used as a literal?
# So it's processed as an instance with a hash and such.
end

def test_hash_is_unordered
hash1 = { :one => "uno", :two => "dos" }
hash2 = { :two => "dos", :one => "uno" }

assert_equal __, hash1 == hash2
assert_equal true, hash1 == hash2
end

def test_hash_keys
hash = { :one => "uno", :two => "dos" }
assert_equal __, hash.keys.size
assert_equal __, hash.keys.include?(:one)
assert_equal __, hash.keys.include?(:two)
assert_equal __, hash.keys.class
assert_equal 2, hash.keys.size
assert_equal true, hash.keys.include?(:one)
assert_equal true, hash.keys.include?(:two)
assert_equal Array, hash.keys.class
end

def test_hash_values
hash = { :one => "uno", :two => "dos" }
assert_equal __, hash.values.size
assert_equal __, hash.values.include?("uno")
assert_equal __, hash.values.include?("dos")
assert_equal __, hash.values.class
assert_equal 2, hash.values.size
assert_equal true, hash.values.include?("uno")
assert_equal true, hash.values.include?("dos")
assert_equal Array, hash.values.class
end

def test_combining_hashes
hash = { "jim" => 53, "amy" => 20, "dan" => 23 }
new_hash = hash.merge({ "jim" => 54, "jenny" => 26 })

assert_equal __, hash != new_hash
assert_equal true, hash != new_hash

expected = { "jim" => __, "amy" => 20, "dan" => 23, "jenny" => __ }
assert_equal __, expected == new_hash
expected = { "jim" => 54, "amy" => 20, "dan" => 23, "jenny" => 26 }
assert_equal true, expected == new_hash
end

def test_default_value
hash1 = Hash.new
hash1[:one] = 1

assert_equal __, hash1[:one]
assert_equal __, hash1[:two]
assert_equal 1, hash1[:one]
assert_equal nil, hash1[:two]

hash2 = Hash.new("dos")
hash2[:one] = 1

assert_equal __, hash2[:one]
assert_equal __, hash2[:two]
assert_equal 1, hash2[:one]
assert_equal "dos", hash2[:two]
end

def test_default_value_is_the_same_object
Expand All @@ -96,11 +98,11 @@ def test_default_value_is_the_same_object
hash[:one] << "uno"
hash[:two] << "dos"

assert_equal __, hash[:one]
assert_equal __, hash[:two]
assert_equal __, hash[:three]
assert_equal ["uno","dos"], hash[:one]
assert_equal ["uno","dos"], hash[:two]
assert_equal ["uno","dos"], hash[:three]

assert_equal __, hash[:one].object_id == hash[:two].object_id
assert_equal true, hash[:one].object_id == hash[:two].object_id
end

def test_default_value_with_block
Expand All @@ -109,8 +111,8 @@ def test_default_value_with_block
hash[:one] << "uno"
hash[:two] << "dos"

assert_equal __, hash[:one]
assert_equal __, hash[:two]
assert_equal __, hash[:three]
assert_equal ["uno"], hash[:one]
assert_equal ["dos"], hash[:two]
assert_equal [], hash[:three]
end
end

0 comments on commit 85af231

Please sign in to comment.