Permalink
Browse files

working

  • Loading branch information...
Danish Khan
Danish Khan committed Apr 3, 2012
1 parent 94f8d6b commit fe7d0f2471bdbe4fab483c82c702f4e004097746
Showing with 30 additions and 22 deletions.
  1. +12 −12 koans/about_blocks.rb
  2. +5 −5 koans/about_iteration.rb
  3. +10 −5 koans/about_sandwich_code.rb
  4. +3 −0 koans/about_scoring_project.rb
View
@@ -8,12 +8,12 @@ def method_with_block
def test_methods_can_take_blocks
yielded_result = method_with_block { 1 + 2 }
- assert_equal __, yielded_result
+ assert_equal 3, yielded_result
end
def test_blocks_can_be_defined_with_do_end_too
yielded_result = method_with_block do 1 + 2 end
- assert_equal __, yielded_result
+ assert_equal 3, yielded_result
end
# ------------------------------------------------------------------
@@ -24,7 +24,7 @@ def method_with_block_arguments
def test_blocks_can_take_arguments
result = method_with_block_arguments do |argument|
- assert_equal __, argument
+ assert_equal "Jim", argument
end
end
@@ -40,7 +40,7 @@ def many_yields
def test_methods_can_call_yield_many_times
result = []
many_yields { |item| result << item }
- assert_equal __, result
+ assert_equal [:peanut, :butter, :and, :jelly], result
end
# ------------------------------------------------------------------
@@ -54,30 +54,30 @@ def yield_tester
end
def test_methods_can_see_if_they_have_been_called_with_a_block
- assert_equal __, yield_tester { :with_block }
- assert_equal __, yield_tester
+ assert_equal :with_block, yield_tester { :with_block }
+ assert_equal :no_block, yield_tester
end
# ------------------------------------------------------------------
def test_block_can_affect_variables_in_the_code_where_they_are_created
value = :initial_value
method_with_block { value = :modified_in_a_block }
- assert_equal __, value
+ assert_equal :modified_in_a_block, value
end
def test_blocks_can_be_assigned_to_variables_and_called_explicitly
add_one = lambda { |n| n + 1 }
- assert_equal __, add_one.call(10)
+ assert_equal 11, add_one.call(10)
# Alternative calling sequence
- assert_equal __, add_one[10]
+ assert_equal 11, add_one[10]
end
def test_stand_alone_blocks_can_be_passed_to_methods_expecting_blocks
make_upper = lambda { |n| n.upcase }
result = method_with_block_arguments(&make_upper)
- assert_equal __, result
+ assert_equal "JIM", result
end
# ------------------------------------------------------------------
@@ -87,10 +87,10 @@ def method_with_explicit_block(&block)
end
def test_methods_can_take_an_explicit_block_argument
- assert_equal __, method_with_explicit_block { |n| n * 2 }
+ assert_equal 20, method_with_explicit_block { |n| n * 2 }
add_one = lambda { |n| n + 1 }
- assert_equal __, method_with_explicit_block(&add_one)
+ assert_equal 11, method_with_explicit_block(&add_one)
end
end
View
@@ -58,15 +58,15 @@ def test_select_selects_certain_items_from_an_array
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) { |sum, item| sum * item }
- assert_equal __, result2
+ assert_equal 24, result2
# Extra Credit:
# Describe in your own words what inject does.
@@ -75,12 +75,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,
@@ -14,7 +14,7 @@ def count_lines(file_name)
end
def test_counting_lines
- assert_equal __, count_lines("example_file.txt")
+ assert_equal 4, count_lines("example_file.txt")
end
# ------------------------------------------------------------------
@@ -29,7 +29,7 @@ def find_line(file_name)
end
def test_finding_lines
- assert_equal __, find_line("example_file.txt")
+ assert_equal "test\n", find_line("example_file.txt")
end
# ------------------------------------------------------------------
@@ -74,17 +74,22 @@ def count_lines2(file_name)
end
def test_counting_lines2
- assert_equal __, count_lines2("example_file.txt")
+ assert_equal 4, count_lines2("example_file.txt")
end
# ------------------------------------------------------------------
def find_line2(file_name)
# Rewrite find_line using the file_sandwich library function.
+ file_sandwich(file_name) do |file|
+ while line = file.gets
+ return line if line.match(/e/)
+ end
+ end
end
def test_finding_lines2
- assert_equal __, find_line2("example_file.txt")
+ assert_equal "test\n", find_line2("example_file.txt")
end
# ------------------------------------------------------------------
@@ -100,7 +105,7 @@ def count_lines3(file_name)
end
def test_open_handles_the_file_sandwich_when_given_a_block
- assert_equal __, count_lines3("example_file.txt")
+ assert_equal 4, count_lines3("example_file.txt")
end
end
@@ -31,6 +31,9 @@
def score(dice)
# You need to write this method
+ if dice == []
+ return 0
+ end
end
class AboutScoringProject < EdgeCase::Koan

0 comments on commit fe7d0f2

Please sign in to comment.