Created
January 18, 2017 13:42
-
-
Save lbvf50mobile/0c323fc592c45ca37266232199885362 to your computer and use it in GitHub Desktop.
ruby_koans_hashes
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
require File.expand_path(File.dirname(__FILE__) + '/neo') | |
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 | |
end | |
def test_hash_literals | |
hash = { :one => "uno", :two => "dos" } | |
assert_equal __, 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] | |
end | |
def test_accessing_hashes_with_fetch | |
hash = { :one => "uno" } | |
assert_equal __, hash.fetch(:one) | |
assert_raise(___) do | |
hash.fetch(:doesnt_exist) | |
end | |
# THINK ABOUT IT: | |
# | |
# Why might you want to use #fetch instead of #[] when accessing hash keys? | |
end | |
def test_changing_hashes | |
hash = { :one => "uno", :two => "dos" } | |
hash[:one] = "eins" | |
expected = { :one => __, :two => "dos" } | |
assert_equal __, hash | |
# Bonus Question: Why was "expected" broken out into a variable | |
# rather than used as a literal? | |
end | |
def test_hash_is_unordered | |
hash1 = { :one => "uno", :two => "dos" } | |
hash2 = { :two => "dos", :one => "uno" } | |
assert_equal __, 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 | |
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 | |
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 | |
expected = { "jim" => __, "amy" => 20, "dan" => 23, "jenny" => __ } | |
assert_equal __, expected == new_hash | |
end | |
def test_default_value | |
hash1 = Hash.new | |
hash1[:one] = 1 | |
assert_equal __, hash1[:one] | |
assert_equal __, hash1[:two] | |
hash2 = Hash.new("dos") | |
hash2[:one] = 1 | |
assert_equal __, hash2[:one] | |
assert_equal __, hash2[:two] | |
end | |
def test_default_value_is_the_same_object | |
hash = Hash.new([]) | |
hash[:one] << "uno" | |
hash[:two] << "dos" | |
assert_equal __, hash[:one] | |
assert_equal __, hash[:two] | |
assert_equal __, hash[:three] | |
assert_equal __, hash[:one].object_id == hash[:two].object_id | |
end | |
def test_default_value_with_block | |
hash = Hash.new {|hash, key| hash[key] = [] } | |
hash[:one] << "uno" | |
hash[:two] << "dos" | |
assert_equal __, hash[:one] | |
assert_equal __, hash[:two] | |
assert_equal __, hash[:three] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment