Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Created August 22, 2014 22:01
Show Gist options
  • Save hayduke19us/8a639c48575bab32ea42 to your computer and use it in GitHub Desktop.
Save hayduke19us/8a639c48575bab32ea42 to your computer and use it in GitHub Desktop.
require "minitest/autorun"
class Archeology
#The method 'function' takes a single argument. The argument must be a collection,
#or at least something enumerble since the inject method is used. Inject's
#argument is an accumulator the block proceding has two interpolations. The
#first is injects accumulator, an empty hash. The second "val" will iterate thru
#the collection given to the function method. The block of code that follows
#does something really funny. It begins to populate the empty hash or
#accumulator with keys and values. The keys come from the iterated
#collection. The corresponding values also come from iterated collection however,
#the hash[val] method is looking for the value of the first key in the new hash.
#Because that key has not been defined yet it becomes nil. This is where it's a
#bit cheeky. When nil.to_i it equals 0 and then with the addition of 1 every
#value in the hash will be O. Then the hash os returned to begin the next
#iteration.
#The last part of the function method uses the
#.reject method. This simply takes a defining block of code and applies that
#delimeter to the collection sent to reject. Here, it iterates thru our hash
#and if a val is equal to 1 then it rejects or removes the key value pair.
#Last but not least .keys method just returns an array of keys extracted from
#the hash. Unfortunatelly for us this returns an empty array since all of the
#values in hash were rejected. So all said and done def function returns an
#empty array regardles of what kind of collection you send it. Technically the
#simplest rendition of function would be:
def empty_arr
[]
end
#A cleaned up version that still takes a collection for an argument,
#creates and populates the hash with inject, applies the addition of the integer 1
#to the values of the hash. Which like we discussed is nil + 1 = 0. Then reject
#removes all the key value pairs if the values are equal to 0 and returns an
#array of keys if any exist
def function arr
arr.inject({}) { |hash, val| hash[val] = hash[val].to_i + 1; hash}\
.reject {|key, val| val == 1}.keys
end
end
class TestArcheology < MiniTest::Test
def setup
@arch = Archeology.new
@hash = {matt: "rubyist", neil: "astronaout", robin: "comedian"}
@int_arr = [0, 1, 2, 3, 4, 5, 6]
@str_arr = %w[franny zooey buddy seymore]
@mixed_arr = ["henry", nil, 0]
end
def test_that_empty_arr_returns_an_empty_array
assert_equal [], @arch.empty_arr
end
def test_if_function_method_is_sent_a_hash_it_returns_an_empty_array
assert_equal [], @arch.function(@hash)
end
def test_if_function_method_is_sent_an_array_of_strings_it_returns_an_empty_array
assert_equal [], @arch.function(@str_arr)
end
def test_if_function_method_is_sent_an_array_of_integers_it_returns_an_empty_array
assert_equal [], @arch.function(@int_arr)
end
def test_if_function_method_is_sent_an_array_with_nil_values_it_returns_an_empty_array
assert_equal [], @arch.function(@mixed_arr)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment