mirror of
https://github.com/edgecase/ruby_koans.git
synced 2026-04-15 07:23:19 -04:00
moving to a new repo
This commit is contained in:
56
about_hashes.rb
Normal file
56
about_hashes.rb
Normal file
@@ -0,0 +1,56 @@
|
||||
require 'edgecase'
|
||||
|
||||
class AboutHashes < EdgeCase::Koan
|
||||
def test_creating_hashes
|
||||
empty_hash = Hash.new
|
||||
assert_equal Hash, 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_changing_hashes
|
||||
hash = { :one => "uno", :two => "dos" }
|
||||
hash[:one] = "eins"
|
||||
|
||||
expected = { :one => __, :two => "dos" }
|
||||
assert_equal expected, 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_and_values
|
||||
hash = { :one => "uno", :two => "dos" }
|
||||
assert_equal __, hash.keys
|
||||
assert_equal __, hash.values
|
||||
end
|
||||
|
||||
def test_combining_hashes
|
||||
hash = { "jim" => 53, "amy" => 20, "dan" => 23 }
|
||||
new_hash = hash.merge({ "jim" => 54, "jenny" => 26 })
|
||||
|
||||
assert_not_equal hash, new_hash
|
||||
|
||||
expected = { "jim" => __, "amy" => 20, "dan" => 23, "jenny" => __ }
|
||||
assert_equal expected, new_hash
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user