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