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/blocks #2

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,1,2,3,4,5,6,6,6,6,6,7,8
24 changes: 12 additions & 12 deletions about_blocks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

# ------------------------------------------------------------------
Expand All @@ -24,7 +24,7 @@ def method_with_block_arguments

def test_blocks_can_take_arguments
method_with_block_arguments do |argument|
assert_equal __, argument
assert_equal "Jim", argument
end
end

Expand All @@ -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

# ------------------------------------------------------------------
Expand All @@ -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 syntax
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

# ------------------------------------------------------------------
Expand All @@ -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