从方法内调用 next 进行外循环
Call next for outer loop from within a method
是否可以在外循环的方法中调用 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
是的。您应该使用 throw
和 catch
.
bot.rb
while Option.daemon_active?(daemon: "bot")
catch(:foo) do
...
Trade.market_order
...
end
end
trade.rb
class Trade
def self.market_order
...
response = exchange.market_sell
throw :foo if response["state"] == false
end
end
是否可以在外循环的方法中调用 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
是的。您应该使用 throw
和 catch
.
bot.rb
while Option.daemon_active?(daemon: "bot")
catch(:foo) do
...
Trade.market_order
...
end
end
trade.rb
class Trade
def self.market_order
...
response = exchange.market_sell
throw :foo if response["state"] == false
end
end