在 Ruby 中重构冗余 each/reverse_each 代码的最佳方法是什么?
What's the best way to refactor redundant each/reverse_each code in Ruby?
我已经为双向链表实现了这些 each
和 reverse_each
方法。
重构它的最佳方法是什么?
def each
return enum_for(:each) unless block_given?
node = self
until node.nil?
yield node
node = node.next
end
end
def reverse_each
return enum_for(:reverse_each) unless block_given?
node = self
until node.nil?
yield node
node = node.prev
end
end
像这样:
{ each: :next , reverse_each: :prev }.each_pair do |name, _method|
define_method(name) do |&blk|
return enum_for(name) unless block_given?
node = self
until node.nil?
blk.call(node)
node = node.send(_method)
end
end
end
删除冗余代码的最简单方法是将其移至新方法。
像这样的东西应该可以工作:
def each(&block)
return enum_for(:each) unless block_given?
traverse(:next, &block)
end
def reverse_each(&block)
return enum_for(:reverse_each) unless block_given?
traverse(:prev, &block)
end
private
def traverse(direction)
node = self
until node.nil?
yield node
node = node.send(direction)
end
end
我已经为双向链表实现了这些 each
和 reverse_each
方法。
重构它的最佳方法是什么?
def each
return enum_for(:each) unless block_given?
node = self
until node.nil?
yield node
node = node.next
end
end
def reverse_each
return enum_for(:reverse_each) unless block_given?
node = self
until node.nil?
yield node
node = node.prev
end
end
像这样:
{ each: :next , reverse_each: :prev }.each_pair do |name, _method|
define_method(name) do |&blk|
return enum_for(name) unless block_given?
node = self
until node.nil?
blk.call(node)
node = node.send(_method)
end
end
end
删除冗余代码的最简单方法是将其移至新方法。
像这样的东西应该可以工作:
def each(&block)
return enum_for(:each) unless block_given?
traverse(:next, &block)
end
def reverse_each(&block)
return enum_for(:reverse_each) unless block_given?
traverse(:prev, &block)
end
private
def traverse(direction)
node = self
until node.nil?
yield node
node = node.send(direction)
end
end