从方法中调用next for outer loop

时间:2015-04-21 09:58:08

标签: ruby loops next

是否可以在外循环的方法中调用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

1 个答案:

答案 0 :(得分:3)

是。您应该使用throwcatch

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