Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

koan/iteration #3

Open
wants to merge 9 commits into
base: Oscar
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .path_progress
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1,2,2,3,1,1,1,2,3,4,4,4,4,6,7,0,1,2,2,2,2,3,3,4,4,4,5,6,6,6,6,7,7,8,9,10,11,11,12,13,13,0,1,2,3,3,4,5,6,6,7,8,9,10,10,10,10,11,11,11,11,1,2,3,3,3,4,4,4,5,6,7,8,8,9,9,10,11,12,13,13,13,14,15,16,17,18,18,19,20,20,21,21,22,22,22,22,23,24,24,25,25,26
1,2,2,3,1,1,1,2,3,4,4,4,4,6,7,0,1,2,2,2,2,3,3,4,4,4,5,6,6,6,6,7,7,8,9,10,11,11,12,13,13,0,1,2,3,3,4,5,6,6,7,8,9,10,10,10,10,11,11,11,11,1,2,3,3,3,4,4,4,5,6,7,8,8,9,9,10,11,12,13,13,13,14,15,16,17,18,18,19,20,20,21,21,22,22,22,22,23,24,24,25,25,26,0,1,2,3,4,4,4,5,5,6,6,7,8,8
26 changes: 13 additions & 13 deletions about_iteration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def as_name(name)
# -------------------------------------------------------------------

def test_each_is_a_method_on_arrays
assert_equal __, [].methods.include?(as_name(:each))
assert_equal true, [].methods.include?(as_name(:each))
end

def test_iterating_with_each
Expand All @@ -33,14 +33,14 @@ def test_iterating_with_each
array.each do |item|
sum += item
end
assert_equal __, sum
assert_equal 6, sum
end

def test_each_can_use_curly_brace_blocks_too
array = [1, 2, 3]
sum = 0
array.each { |item| sum += item }
assert_equal __, sum
assert_equal 6, sum
end

def test_break_works_with_each_style_iterations
Expand All @@ -50,42 +50,42 @@ def test_break_works_with_each_style_iterations
break if item > 3
sum += item
end
assert_equal __, sum
assert_equal 6, sum
end

def test_collect_transforms_elements_of_an_array
array = [1, 2, 3]
new_array = array.collect { |item| item + 10 }
assert_equal __, new_array
assert_equal [11, 12, 13], new_array

# NOTE: 'map' is another name for the 'collect' operation
another_array = array.map { |item| item + 10 }
assert_equal __, another_array
assert_equal [11, 12, 13], another_array
end

def test_select_selects_certain_items_from_an_array
array = [1, 2, 3, 4, 5, 6]

even_numbers = array.select { |item| (item % 2) == 0 }
assert_equal __, even_numbers
assert_equal [2, 4, 6], even_numbers

# NOTE: 'find_all' is another name for the 'select' operation
more_even_numbers = array.find_all { |item| (item % 2) == 0 }
assert_equal __, more_even_numbers
assert_equal [2, 4, 6], more_even_numbers
end

def test_find_locates_the_first_element_matching_a_criteria
array = ["Jim", "Bill", "Clarence", "Doug", "Eli"]

assert_equal __, array.find { |item| item.size > 4 }
assert_equal "Clarence", array.find { |item| item.size > 4 }
end

def test_inject_will_blow_your_mind
result = [2, 3, 4].inject(0) { |sum, item| sum + item }
assert_equal __, result
assert_equal 9, result

result2 = [2, 3, 4].inject(1) { |product, item| product * item }
assert_equal __, result2
assert_equal 24, result2

# Extra Credit:
# Describe in your own words what inject does.
Expand All @@ -94,12 +94,12 @@ def test_inject_will_blow_your_mind
def test_all_iteration_methods_work_on_any_collection_not_just_arrays
# Ranges act like a collection
result = (1..3).map { |item| item + 10 }
assert_equal __, result
assert_equal [11, 12, 13], result

# Files act like a collection of lines
File.open("example_file.txt") do |file|
upcase_lines = file.map { |line| line.strip.upcase }
assert_equal __, upcase_lines
assert_equal ["THIS", "IS", "A", "TEST"], upcase_lines
end

# NOTE: You can create your own collections that work with each,
Expand Down