1 require File.expand_path(File.dirname(__FILE__) + '/edgecase')
3 class AboutIteration < EdgeCase::Koan
5 # -- An Aside ------------------------------------------------------
6 # Ruby 1.8 stores names as strings. Ruby 1.9 stores names as
7 # symbols. So we use a version dependent method "as_name" to convert
8 # to the right format in the koans. We will use "as_name" whenever
9 # comparing to lists of methods.
11 in_ruby_version("1.8") do
17 in_ruby_version("1.9") do
23 # Ok, now back to the Koans.
24 # -------------------------------------------------------------------
26 def test_each_is_a_method_on_arrays
27 assert_equal __, [].methods.include?(as_name(:each))
30 def test_iterating_with_each
39 def test_each_can_use_curly_brace_blocks_too
48 def test_break_works_with_each_style_iterations
49 array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
58 def test_collect_transforms_elements_of_an_array
60 new_array = array.collect { |item| item + 10 }
61 assert_equal __, new_array
63 # NOTE: 'map' is another name for the 'collect' operation
64 another_array = array.map { |item| item + 10 }
65 assert_equal __, another_array
68 def test_select_selects_certain_items_from_an_array
69 array = [1, 2, 3, 4, 5, 6]
71 even_numbers = array.select { |item| (item % 2) == 0 }
72 assert_equal __, even_numbers
74 # NOTE: 'find_all' is another name for the 'select' operation
75 more_even_numbers = array.find_all { |item| (item % 2) == 0 }
76 assert_equal __, more_even_numbers
79 def test_find_locates_the_first_element_matching_a_criteria
80 array = ["Jim", "Bill", "Clarence", "Doug", "Eli"]
82 assert_equal __, array.find { |item| item.size > 4 }
85 def test_inject_will_blow_your_mind
86 result = [2, 3, 4].inject(0) { |sum, item| sum + item }
87 assert_equal __, result
89 result2 = [2, 3, 4].inject(1) { |sum, item| sum * item }
90 assert_equal __, result2
93 # Describe in your own words what inject does.
96 def test_all_iteration_methods_work_on_any_collection_not_just_arrays
97 # Ranges act like a collection
98 result = (1..3).map { |item| item + 10 }
99 assert_equal __, result
101 # Files act like a collection of lines
102 File.open("example_file.txt") do |file|
103 upcase_lines = file.map { |line| line.strip.upcase }
104 assert_equal __, upcase_lines
107 # NOTE: You can create your own collections that work with each,
111 # Bonus Question: In the previous koan, we saw the construct:
113 # File.open(filename) do |file|
114 # # code to read 'file'
117 # Why did we do it that way instead of the following?
119 # file = File.open(filename)
120 # # code to read 'file'
122 # When you get to the "AboutSandwichCode" koan, recheck your answer.