Skip to content

Commit

Permalink
p.163 Change the parameter of "count_descendants_matching" to block.
Browse files Browse the repository at this point in the history
  • Loading branch information
kono committed Jun 12, 2010
1 parent b4a208f commit 2280f0e
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions chapter6/Extract_Surrounding_Method.rb
Expand Up @@ -20,18 +20,18 @@ 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?
@date_of_death.nil?
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

0 comments on commit 2280f0e

Please sign in to comment.