From 2280f0e465d582bbf9b895eba20c96abe77abaff Mon Sep 17 00:00:00 2001 From: Hiroshi Kono Date: Sat, 12 Jun 2010 19:15:12 +0900 Subject: [PATCH] p.163 Change the parameter of "count_descendants_matching" to block. --- chapter6/Extract_Surrounding_Method.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/chapter6/Extract_Surrounding_Method.rb b/chapter6/Extract_Surrounding_Method.rb index b9bd2d0..25a67ce 100644 --- a/chapter6/Extract_Surrounding_Method.rb +++ b/chapter6/Extract_Surrounding_Method.rb @@ -20,7 +20,7 @@ def number_of_living_descendants end def number_of_descendants_named(name) - count_descendants_matching(name) + count_descendants_matching{|descendant| descendant.name == name} end def alive? @@ -28,10 +28,10 @@ def alive? end protected - def count_descendants_matching(name) + def count_descendants_matching(&block) children.inject(0) do |count, child| - count += 1 if child.name == name - count += child.count_descendants_matching(name) + count += 1 if yield child + count += child.count_descendants_matching(&block) end end end