1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 class AboutIteration < EdgeCase::Koan
4 in_ruby_version("1.8") do
5 def test_each_is_a_method_on_arrays
6 assert_equal __, [].methods.include?("each")
10 in_ruby_version("1.9") do
11 def test_each_is_a_method_on_arrays
12 assert_equal __, [].methods.include?(:each)
16 def test_iterating_with_each
25 def test_each_can_use_curly_brace_blocks_too
34 def test_break_works_with_each_style_iterations
35 array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
44 def test_collect_transforms_elements_of_an_array
46 new_array = array.collect { |item| item + 10 }
47 assert_equal __, new_array
49 # NOTE: 'map' is another name for the 'collect' operation
50 another_array = array.map { |item| item + 10 }
51 assert_equal __, another_array
54 def test_select_selects_certain_items_from_an_array
55 array = [1, 2, 3, 4, 5, 6]
57 even_numbers = array.select { |item| (item % 2) == 0 }
58 assert_equal __, even_numbers
60 # NOTE: 'find_all' is another name for the 'select' operation
61 more_even_numbers = array.find_all { |item| (item % 2) == 0 }
62 assert_equal __, more_even_numbers
65 def test_find_locates_the_first_element_matching_a_criteria
66 array = ["Jim", "Bill", "Clarence", "Doug", "Eli"]
68 assert_equal __, array.find { |item| item.size > 4 }
71 def test_inject_will_blow_your_mind
72 result = [2, 3, 4].inject(0) { |sum, item| sum + item }
73 assert_equal __, result
75 result2 = [2, 3, 4].inject(1) { |product, item| product * item }
76 assert_equal __, result2
79 # Describe in your own words what inject does.
82 def test_all_iteration_methods_work_on_any_collection_not_just_arrays
83 # Ranges act like a collection
84 result = (1..3).map { |item| item + 10 }
85 assert_equal __, result
87 # Files act like a collection of lines
88 File.open("example_file.txt") do |file|
89 upcase_lines = file.map { |line| line.strip.upcase }
90 assert_equal __, upcase_lines
93 # NOTE: You can create your own collections that work with each,
97 # Bonus Question: In the previous koan, we saw the construct:
99 # File.open(filename) do |file|
100 # # code to read 'file'
103 # Why did we do it that way instead of the following?
105 # file = File.open(filename)
106 # # code to read 'file'
108 # When you get to the "AboutSandwichCode" koan, recheck your answer.