将三种相似的方法变成一种方法
我想将这三种方法合二为一,但目前我无法思考。 1/3 需要稍微不同的调用,这一事实使情况变得复杂。它们都很相似,我知道有更好的方法,但超出了我现在的技能水平。通过传入一个额外的变量(获取、检查或处理),我可以将其变成一个,但我不知道如何做到这一点。
如果您要将这些重构为一种方法,您会怎么做?
def fetch(subjects = current_subjects, queues = QUEUES)
subjects.each do |s|
queues.each { |x| fetch_results(x, s) } unless queue.nil?
end
end
def check(subjects = current_subjects, queues = QUEUES)
subjects.each do |s|
queues.each { |x| check_results(s["#{x}_recent"]) } unless queue.nil?
end
end
def process(subjects = current_subjects, queues = QUEUES)
subjects.each do |s|
queues.each { |x| process_results(s["#{x}_recent"]) } unless queue.nil?
end
end
编辑:一种解决方案与我之前的想法很接近,但我没有明确表示我想将 what
作为一个小数组传递,它可能是可扩展的并可用于指示是否获取、检查、处理或这些的任意组合。因此,本质上,我试图用一种方法循环执行三件事:
- 一个操作:IE、获取、检查或处理。
- 任意数量的科目。
- 任意数量的队列,目前是一个常量。
另外,这里还有其他解决方案:
I want to turn these three methods into one, but am unable to wrap my brain around it at this moment. Complicated by the fact that 1/3 requires a slightly different call. They're all similar enough, and I know there is a better way, but beyond my skill level right now. With one extra variable passed in (fetch, check, or process) I could turn it into one, how to do this eludes me.
If you were to refactor these into one method, how would you do it?
def fetch(subjects = current_subjects, queues = QUEUES)
subjects.each do |s|
queues.each { |x| fetch_results(x, s) } unless queue.nil?
end
end
def check(subjects = current_subjects, queues = QUEUES)
subjects.each do |s|
queues.each { |x| check_results(s["#{x}_recent"]) } unless queue.nil?
end
end
def process(subjects = current_subjects, queues = QUEUES)
subjects.each do |s|
queues.each { |x| process_results(s["#{x}_recent"]) } unless queue.nil?
end
end
EDIT: One solution is close to what I was thinking earlier, but I didn't make it clear that I want to pass in the what
as a smallish array, that might be expandable and can used to indicate whether to fetch, check, or process or any combination of those. So, essentially, I'm trying to loop through three things with one method:
- an action what: I.E., fetch, check, or process.
- any number of subjects.
- any number of queues, which is a constant at the moment.
Also, other solutions here:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@Lucapette 提出了一个自上而下的解决方案(我认为这在大多数情况下非常有效)。然而,@Tony 正确地指出,方法可能会演变,因此它可能过于僵化。另一种解决方案是自下而上的方法:
其他方法同上。顺便说一句,双精度
each
也可以写成:@Lucapette proposes a top-down solution (which I think it's pretty valid on most cases). However, @Tony correctly points out that methods may evolve and so it may be too rigid. The alternative solution is a bottom-up approach:
Ditto for the other methods. BTW, that double
each
can also be written:是一种方法。当然,命名由你决定。
is a way to do it. Of course, naming is up to you.
我可能会做这样的事情:
I might do something like this:
为什么要将它们重构为单个函数?它们现在很相似,但如果它们后来进化并变得不同怎么办?每个函数都有其自己的用途,应将其保留为单独的函数。
Why do you want to refactor them into a single function? They are similar now, but what if they evolve later on and become different? Each function has it's own purpose and should be left as individual functions.