591b869b1eb7460beaee2db83c5819c2a6656f57
[ruby_koans.git] / koans / about_iteration.rb
1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
2
3 class AboutIteration < EdgeCase::Koan
4
5   def test_each_is_a_method_on_arrays
6     [].methods.include?("each")
7   end
8
9   def test_iterating_with_each
10     array = [1, 2, 3]
11     sum = 0
12     array.each do |item|
13       sum += item
14     end
15     assert_equal __, sum
16   end
17
18   def test_each_can_use_curly_brace_blocks_too
19     array = [1, 2, 3]
20     sum = 0
21     array.each { |item|
22       sum += item
23     }
24     assert_equal __, sum
25   end
26
27   def test_break_works_with_each_style_iterations
28     array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
29     sum = 0
30     array.each { |item|
31       break if item > 3
32       sum += item
33     }
34     assert_equal __, sum
35   end
36
37   def test_collect_transforms_elements_of_an_array
38     array = [1, 2, 3]
39     new_array = array.collect { |item| item + 10 }
40     assert_equal __, new_array
41
42     # NOTE: 'map' is another name for the 'collect' operation
43     another_array = array.map { |item| item + 10 }
44     assert_equal __, another_array
45   end
46
47   def test_select_selects_certain_items_from_an_array
48     array = [1, 2, 3, 4, 5, 6]
49
50     even_numbers = array.select { |item| (item % 2) == 0 }
51     assert_equal __, even_numbers
52
53     # NOTE: 'find_all' is another name for the 'select' operation
54     more_even_numbers = array.find_all { |item| (item % 2) == 0 }
55     assert_equal __, more_even_numbers
56   end
57
58   def test_find_locates_the_first_element_matching_a_criteria
59     array = ["Jim", "Bill", "Clarence", "Doug", "Eli"]
60
61     assert_equal __, array.find { |item| item.size > 4 }
62   end
63
64   def test_inject_will_blow_your_mind
65     result = [2, 3, 4].inject(0) { |sum, item| sum + item }
66     assert_equal __, result
67
68     result2 = [2, 3, 4].inject(1) { |sum, item| sum * item }
69     assert_equal __, result2
70
71     # Extra Credit:
72     # Describe in your own words what inject does.
73   end
74
75   def test_all_iteration_methods_work_on_any_collection_not_just_arrays
76     # Ranges act like a collection
77     result = (1..3).map { |item| item + 10 }
78     assert_equal __, result
79
80     # Files act like a collection of lines
81     File.open("example_file.txt") do |file|
82       upcase_lines = file.map { |line| line.strip.upcase }
83       assert_equal __, upcase_lines
84     end
85
86     # NOTE: You can create your own collections that work with each,
87     # map, select, etc.
88   end
89
90   # Bonus Question:  In the previous koan, we saw the construct:
91   #
92   #   File.open(filename) do |file|
93   #     # code to read 'file'
94   #   end
95   #
96   # Why did we do it that way instead of the following?
97   #
98   #   file = File.open(filename)
99   #   # code to read 'file'
100   #
101   # When you get to the "AboutSandwichCode" koan, recheck your answer.
102
103 end