2
我一直在阅读Ruby重构书(Fields,Harvie,Fowler)。他们提到Extract Surrounding Method操作,如果您有中间部分彼此不同的方法,可以使用它来避免重复。Javascript版本的提取周边方法
def number_of_descendants_named(name)
count_descendants_matchin { |descendant| descendant.name == name }
end
def number_of_living_descendants
count_descendants_matching { |descendant| descendant.alive? }
end
def count_descendants_mathing(&block)
children.inject(0) do |count, child|
count += 1 if yield child
count + child.count_descendants_matching(&block)
end
end
我相信你明白了。你会怎么做类似于Javascript?
感谢您的快速回答!这正是我所期待的! – hade 2012-08-09 06:55:33