【发布时间】:2015-06-28 10:01:26
【问题描述】:
是否可以从外部循环的方法中调用 next:
bot.rb
while Option.daemon_active?(daemon: "bot")
.....
Trade.market_order
....
end
trade.rb
class Trade
def self.market_order
... complex code ...
response = exchange.market_sell
next if response["state"] == false # This fails. This should start new iteration of while in bot.rb
end
end
有一个非常相似的问题,但似乎不适合我:call next on ruby loop from external method
【问题讨论】:
-
while 块中的
next unless Trade.market_order有什么问题? -
同意@mudasobwa 不应该通过
Trade控制Bot,这根本不是它的责任,并且增加了通常应该避免的耦合(难以遵循执行路径,难以重构,Trade可能会在Bot更改等情况下中断) -
谢谢@mudasobwa。很好的评论,虽然现在市场订单比我在这里显示的要复杂得多,但有很多检查,所有这些都需要在某个时候调用 next。也许我需要简化它
-
return false任何你需要下一次迭代的地方,或者使用@sawa 的方法和catch。